Skip to content

Commit 500d2e9

Browse files
committed
Add reset_color_map()
1 parent c3e618e commit 500d2e9

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fltk-theme"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
authors = ["MoAlyousef <mohammed.alyousef@neurosrg.com>"]
55
edition = "2021"
66
description = "A theming crate for fltk-rs"

examples/predef_color.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use fltk::{prelude::*, *};
2-
use fltk_theme::{color_themes, ColorTheme};
2+
use fltk_theme::{reset_color_map, color_themes, ColorTheme};
33

44
fn main() {
55
let a = app::App::default().with_scheme(app::Scheme::Gtk);
@@ -17,7 +17,8 @@ fn main() {
1717
let mut round = button::RoundButton::new(160, 180, 80, 30, " Round");
1818
round.set_value(true);
1919
round.set_frame(enums::FrameType::FlatBox);
20-
button::Button::new(160, 220, 80, 30, "Hello");
20+
let mut button = button::Button::new(160, 220, 80, 30, "Hello");
21+
button.set_callback(|_| reset_color_map());
2122
win.end();
2223
win.show();
2324
choice.set_callback(|c| {

src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![doc = include_str!("../README.md")]
22
#![allow(clippy::needless_doctest_main)]
33

4-
use fltk::{app, enums::Color};
4+
use fltk::{app, enums::Color, utils::oncelock::OnceCell};
55
pub mod color_themes;
66
pub mod colors;
77
pub mod widget_schemes;
@@ -16,6 +16,18 @@ pub struct ColorMap {
1616
pub b: u8,
1717
}
1818

19+
static DEFAULT_COLOR_MAP: OnceCell<Vec<ColorMap>> = OnceCell::new();
20+
21+
/// Resets the current map to the default color map
22+
pub fn reset_color_map() {
23+
if let Some(old_map) = DEFAULT_COLOR_MAP.get() {
24+
for elem in old_map {
25+
app::set_color(Color::by_index(elem.index), elem.r, elem.g, elem.b);
26+
}
27+
app::redraw();
28+
}
29+
}
30+
1931
#[macro_export]
2032
macro_rules! cmap {
2133
($i:tt, $r:tt, $g:tt, $b:tt) => {
@@ -45,6 +57,14 @@ impl ColorTheme {
4557

4658
/// apply() the theme
4759
pub fn apply(&self) {
60+
if DEFAULT_COLOR_MAP.get().is_none() {
61+
let mut default_map = Vec::with_capacity(256);
62+
for index in 0..=255 {
63+
let (r, g, b) = Color::by_index(index).to_rgb();
64+
default_map.push(ColorMap {index, r, g, b });
65+
}
66+
DEFAULT_COLOR_MAP.set(default_map).unwrap();
67+
}
4868
for elem in &self.0 {
4969
app::set_color(Color::by_index(elem.index), elem.r, elem.g, elem.b);
5070
}

0 commit comments

Comments
 (0)