Skip to content

Commit

Permalink
Add reset_color_map()
Browse files Browse the repository at this point in the history
  • Loading branch information
MoAlyousef committed Aug 14, 2024
1 parent c3e618e commit 500d2e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fltk-theme"
version = "0.7.2"
version = "0.7.3"
authors = ["MoAlyousef <mohammed.alyousef@neurosrg.com>"]
edition = "2021"
description = "A theming crate for fltk-rs"
Expand Down
5 changes: 3 additions & 2 deletions examples/predef_color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fltk::{prelude::*, *};
use fltk_theme::{color_themes, ColorTheme};
use fltk_theme::{reset_color_map, color_themes, ColorTheme};

fn main() {
let a = app::App::default().with_scheme(app::Scheme::Gtk);
Expand All @@ -17,7 +17,8 @@ fn main() {
let mut round = button::RoundButton::new(160, 180, 80, 30, " Round");
round.set_value(true);
round.set_frame(enums::FrameType::FlatBox);
button::Button::new(160, 220, 80, 30, "Hello");
let mut button = button::Button::new(160, 220, 80, 30, "Hello");
button.set_callback(|_| reset_color_map());
win.end();
win.show();
choice.set_callback(|c| {
Expand Down
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = include_str!("../README.md")]
#![allow(clippy::needless_doctest_main)]

use fltk::{app, enums::Color};
use fltk::{app, enums::Color, utils::oncelock::OnceCell};
pub mod color_themes;
pub mod colors;
pub mod widget_schemes;
Expand All @@ -16,6 +16,18 @@ pub struct ColorMap {
pub b: u8,
}

static DEFAULT_COLOR_MAP: OnceCell<Vec<ColorMap>> = OnceCell::new();

/// Resets the current map to the default color map
pub fn reset_color_map() {
if let Some(old_map) = DEFAULT_COLOR_MAP.get() {
for elem in old_map {
app::set_color(Color::by_index(elem.index), elem.r, elem.g, elem.b);
}
app::redraw();
}
}

#[macro_export]
macro_rules! cmap {
($i:tt, $r:tt, $g:tt, $b:tt) => {
Expand Down Expand Up @@ -45,6 +57,14 @@ impl ColorTheme {

/// apply() the theme
pub fn apply(&self) {
if DEFAULT_COLOR_MAP.get().is_none() {
let mut default_map = Vec::with_capacity(256);
for index in 0..=255 {
let (r, g, b) = Color::by_index(index).to_rgb();
default_map.push(ColorMap {index, r, g, b });
}
DEFAULT_COLOR_MAP.set(default_map).unwrap();
}
for elem in &self.0 {
app::set_color(Color::by_index(elem.index), elem.r, elem.g, elem.b);
}
Expand Down

0 comments on commit 500d2e9

Please # to comment.