Skip to content

Commit

Permalink
(Feat) Color Schemas for IFS Fractals
Browse files Browse the repository at this point in the history
  • Loading branch information
xairaven committed Jan 2, 2025
1 parent 03257ee commit 755df9e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 74 deletions.
18 changes: 9 additions & 9 deletions src/fractals/ifs/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fractals::ifs::system::EquationSystem;
use crate::geometry::dot::{Dot, DotBuilder};
use eframe::epaint::Color32;
use crate::ui::styles::colors::ColorScheme;
use rand::distributions::{Distribution, WeightedIndex};
use rand::thread_rng;

Expand All @@ -9,21 +9,21 @@ pub const DEFAULT_RADIUS: f32 = 0.025;

pub struct Model {
systems: Vec<[f32; 7]>,
colors: Vec<Color32>,
color_schemas: Vec<ColorScheme>,

iterations: u32,
radius: f32,
}

impl Model {
pub fn dots(&self) -> Vec<Dot> {
debug_assert!(self.systems.len() == self.colors.len());
debug_assert!(self.systems.len() == self.color_schemas.len());

let mut equations: Vec<EquationSystem> = Vec::new();
for (index, parameters) in self.systems.iter().enumerate() {
equations.push(
EquationSystem::new(*parameters, self.radius)
.with_color(self.colors[index]),
.with_color_scheme(self.color_schemas[index]),
);
}

Expand Down Expand Up @@ -65,7 +65,7 @@ impl Model {

pub struct ModelBuilder {
systems: Vec<[f32; 7]>,
colors: Vec<Color32>,
color_schemas: Vec<ColorScheme>,

iterations: u32,
radius: f32,
Expand All @@ -75,7 +75,7 @@ impl Default for ModelBuilder {
fn default() -> Self {
Self {
systems: vec![],
colors: vec![],
color_schemas: vec![],

iterations: DEFAULT_ITERATIONS,
radius: DEFAULT_RADIUS,
Expand All @@ -89,8 +89,8 @@ impl ModelBuilder {
self
}

pub fn with_colors(mut self, colors: Vec<Color32>) -> Self {
self.colors = colors;
pub fn with_color_schemas(mut self, color_schemas: Vec<ColorScheme>) -> Self {
self.color_schemas = color_schemas;
self
}

Expand All @@ -107,7 +107,7 @@ impl ModelBuilder {
pub fn build(self) -> Model {
Model {
systems: self.systems,
colors: self.colors,
color_schemas: self.color_schemas,
iterations: self.iterations,
radius: self.radius,
}
Expand Down
20 changes: 10 additions & 10 deletions src/fractals/ifs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::fractals::ifs::validation::ValidationError;
use crate::fractals::ifs::{model, validation};
use crate::geometry::dot::Dot;
use crate::ui::components::canvas::CanvasParams;
use crate::ui::styles::colors;
use egui::{Color32, Shape};
use crate::ui::styles::colors::ColorScheme;
use egui::Shape;

pub struct IfsState {
is_initialized: bool,
Expand All @@ -15,7 +15,7 @@ pub struct IfsState {
pub systems: Vec<[f32; 7]>,

pub is_coloring_enabled: bool,
pub colors: Vec<Color32>,
pub color_schemas: Vec<ColorScheme>,

pub iterations: u32,
pub radius_cm: f32,
Expand All @@ -34,7 +34,7 @@ impl Default for IfsState {
systems: vec![DEFAULT_SYSTEM],

is_coloring_enabled: false,
colors: vec![colors::BLACK],
color_schemas: vec![ColorScheme::Standard],

iterations: model::DEFAULT_ITERATIONS,
radius_cm: model::DEFAULT_RADIUS,
Expand All @@ -48,7 +48,7 @@ impl IfsState {
self.is_drawing_requested = false;
self.dots = ModelBuilder::default()
.with_systems(self.systems.clone())
.with_colors(self.colors.clone())
.with_color_schemas(self.color_schemas.clone())
.with_iterations(self.iterations)
.with_radius(self.radius_cm)
.build()
Expand Down Expand Up @@ -92,25 +92,25 @@ impl IfsState {
self.reset_initialization();

self.systems.push(DEFAULT_SYSTEM);
self.colors.push(colors::BLACK);
self.color_schemas.push(ColorScheme::Standard);
}

pub fn push_system(&mut self, system: [f32; 7]) {
self.systems.push(system);
self.colors.push(colors::BLACK);
self.color_schemas.push(ColorScheme::Standard);
}

pub fn remove_system(&mut self, index: usize) {
debug_assert!(self.systems.len() == self.colors.len());
debug_assert!(self.systems.len() == self.color_schemas.len());

self.reset_initialization();

self.systems.remove(index);
self.colors.remove(index);
self.color_schemas.remove(index);
}

pub fn empty_systems(&mut self) {
self.systems = vec![];
self.colors = vec![];
self.color_schemas = vec![];
}
}
13 changes: 6 additions & 7 deletions src/fractals/ifs/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::geometry::dot::{Dot, DotBuilder};
use crate::geometry::point2d::Point2D;
use crate::ui::styles::colors;
use egui::Color32;
use crate::ui::styles::colors::ColorScheme;

pub struct EquationSystem {
a: f32,
Expand All @@ -12,7 +11,7 @@ pub struct EquationSystem {
f: f32,
p: f32,

color: Color32,
color_scheme: ColorScheme,
radius: f32,
}

Expand All @@ -27,13 +26,13 @@ impl EquationSystem {
f: coefficients[5],
p: coefficients[6],

color: colors::BLACK,
color_scheme: ColorScheme::Standard,
radius,
}
}

pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
pub fn with_color_scheme(mut self, color_scheme: ColorScheme) -> Self {
self.color_scheme = color_scheme;
self
}

Expand All @@ -49,7 +48,7 @@ impl EquationSystem {

DotBuilder::default()
.with_center(Point2D::new(x, y))
.with_color(self.color)
.with_color(self.color_scheme.get_color())
.with_radius_centimeters(self.radius)
.build()
}
Expand Down
53 changes: 41 additions & 12 deletions src/fractals/ifs/ui/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use crate::fractals::ifs::serialization;
use crate::io;
use crate::io::filter::FileFilter;
use crate::ui::styles::colors;
use crate::ui::styles::colors::ColorScheme;
use crate::ui::windows::message::MessageWindow;
use crate::ui::windows::Window;
use eframe::epaint::Color32;
use egui::{Button, DragValue, Grid, RichText};

pub struct IfsParametersWindow {
Expand Down Expand Up @@ -56,7 +58,7 @@ impl Window for IfsParametersWindow {
let mut rule_removed: (bool, usize) = (false, 0);

let grid_columns = 8 + if context.ifs_state.is_coloring_enabled {
1
2
} else {
0
};
Expand Down Expand Up @@ -103,22 +105,49 @@ impl Window for IfsParametersWindow {
reset_initialization = true;
};

if ui.button("Remove").clicked() {
rule_removed = (true, index_system);
}

let scheme = &mut context.ifs_state.color_schemas[index_system];
let color = match &scheme {
ColorScheme::Fixed(color) => *color,
_ => colors::BLACK,
};
if context.ifs_state.is_coloring_enabled {
egui::color_picker::color_edit_button_srgba(
ui,
&mut context.ifs_state.colors[index_system],
egui::color_picker::Alpha::Opaque,
);
egui::ComboBox::from_id_salt(format!("ColorParameter{}", index_system))
.selected_text(format!("{}", &scheme))
.show_ui(ui, |ui| {
ui.selectable_value(
scheme,
ColorScheme::Standard,
ColorScheme::Standard.to_string(),
);
ui.selectable_value(
scheme,
ColorScheme::Fixed(color),
ColorScheme::Fixed(Color32::default()).to_string(),
);
ui.selectable_value(
scheme,
ColorScheme::Random,
ColorScheme::Random.to_string(),
);
});

if let ColorScheme::Fixed(color) = scheme {
egui::color_picker::color_edit_button_srgba(
ui,
color,
egui::color_picker::Alpha::Opaque,
);
}
} else {
for color in &mut context.ifs_state.colors {
*color = colors::BLACK;
for color in &mut context.ifs_state.color_schemas {
*color = ColorScheme::Standard;
}
}

if ui.button("Remove").clicked() {
rule_removed = (true, index_system);
}

ui.end_row();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/fractals/lsystem/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fractals::lsystem::state::ColorScheme;
use crate::geometry::line2d::Line2D;
use crate::geometry::point2d::Point2D;
use crate::math::angle::Angle;
use crate::ui::styles::colors::ColorScheme;
use egui::Stroke;
use std::collections::HashMap;

Expand Down
35 changes: 1 addition & 34 deletions src/fractals/lsystem/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use crate::fractals::lsystem::validation;
use crate::fractals::lsystem::validation::ValidationError;
use crate::geometry::line2d::Line2D;
use crate::ui::components::canvas::CanvasParams;
use crate::ui::styles::colors;
use crate::ui::styles::colors::ColorScheme;
use eframe::epaint::Shape;
use egui::Color32;
use rand::Rng;
use std::collections::HashMap;
use strum_macros::Display;

pub struct LSystemState {
is_initialized: bool,
Expand Down Expand Up @@ -146,33 +143,3 @@ impl LSystemState {
self.rules = Vec::with_capacity(3);
}
}

#[derive(Copy, Clone, Display, Default, PartialEq)]
pub enum ColorScheme {
#[strum(serialize = "Fixed")]
Fixed(Color32),

#[strum(serialize = "Random")]
Random,

#[strum(serialize = "Standard (Black)")]
#[default]
Standard,
}

impl ColorScheme {
pub fn get_color(&self) -> Color32 {
match self {
ColorScheme::Fixed(color) => *color,
ColorScheme::Random => {
let mut rng = rand::thread_rng();
Color32::from_rgb(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
)
},
ColorScheme::Standard => colors::BLACK,
}
}
}
3 changes: 2 additions & 1 deletion src/fractals/lsystem/ui/settings.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::context::Context;
use crate::fractals::lsystem::examples::Example;
use crate::fractals::lsystem::serialization;
use crate::fractals::lsystem::state::{ColorScheme, LSystemState};
use crate::fractals::lsystem::state::LSystemState;
use crate::io;
use crate::io::filter::FileFilter;
use crate::ui::styles::colors;
use crate::ui::styles::colors::ColorScheme;
use crate::ui::windows::message::MessageWindow;
use crossbeam::channel::{unbounded, Receiver, Sender};
use egui::{vec2, Button, Color32, DragValue, Grid, RichText, Ui};
Expand Down
32 changes: 32 additions & 0 deletions src/ui/styles/colors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
use egui::Color32;
use rand::Rng;
use strum_macros::Display;

pub const BLACK: Color32 = Color32::from_rgb(0, 0, 0);
pub const DARK_RED: Color32 = Color32::from_rgb(198, 55, 57);
pub const GRAY: Color32 = Color32::from_rgb(200, 200, 200);
pub const LIME: Color32 = Color32::from_rgb(50, 205, 50);
pub const RED: Color32 = Color32::from_rgb(255, 0, 0);
pub const WHITE: Color32 = Color32::from_rgb(255, 255, 255);

#[derive(Copy, Clone, Display, Default, PartialEq)]
pub enum ColorScheme {
#[strum(serialize = "Fixed")]
Fixed(Color32),

#[strum(serialize = "Random")]
Random,

#[strum(serialize = "Standard (Black)")]
#[default]
Standard,
}

impl ColorScheme {
pub fn get_color(&self) -> Color32 {
match self {
ColorScheme::Fixed(color) => *color,
ColorScheme::Random => {
let mut rng = rand::thread_rng();
Color32::from_rgb(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
)
},
ColorScheme::Standard => BLACK,
}
}
}

0 comments on commit 755df9e

Please sign in to comment.