Skip to content

Commit

Permalink
feat: add opencolor helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
suhdonghwi committed May 22, 2021
1 parent 2dde316 commit 9e73fd2
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 107 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = "2.33.3"
toml = "0.5.8"
ggez = "0.5"
float-cmp = "0.8.0"
lazy_static = "1.4.0"

[lib]
name = "neat"
Expand Down
14 changes: 7 additions & 7 deletions examples/helper/graph_visual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ggez::graphics::{self};
use ggez::nalgebra as na;
use rand::Rng;

use super::text::Text;
use super::{opencolor, text::Text};

fn calculate_y(total_count: usize, nth: usize, rect: &graphics::Rect) -> f32 {
let delta = 40.0;
Expand Down Expand Up @@ -36,9 +36,9 @@ impl NodeDrawInfo {

let total_count = graph.input_number() + 1;
let color = if node_data.kind() == NodeKind::Input {
graphics::Color::from_rgb(73, 80, 87)
*opencolor::GRAY7
} else {
graphics::Color::from_rgb(173, 181, 189)
*opencolor::GRAY5
};

NodeDrawInfo {
Expand All @@ -58,7 +58,7 @@ impl NodeDrawInfo {
rect.x as f32 + rect.w as f32 - left_right_space,
calculate_y(total_count, nth, rect),
),
color: graphics::Color::from_rgb(73, 80, 87),
color: *opencolor::GRAY7,
}
}
NodeKind::Hidden => NodeDrawInfo {
Expand All @@ -68,7 +68,7 @@ impl NodeDrawInfo {
),
rng.gen_range(rect.y + 60.0..rect.y + rect.h - 60.0),
),
color: graphics::Color::from_rgb(134, 142, 150),
color: *opencolor::GRAY5,
},
}
}
Expand All @@ -94,9 +94,9 @@ impl EdgeDrawInfo {
to,
width: (7.0 * weight.abs() / max_weight) as f32,
color: if weight > 0.0 {
graphics::Color::from_rgba(81, 207, 102, 150)
opencolor::with_alpha(*opencolor::GREEN5, 0.7)
} else {
graphics::Color::from_rgba(255, 107, 107, 150)
opencolor::with_alpha(*opencolor::RED5, 0.7)
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/helper/main_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use ggez::nalgebra as na;

use neat::network::network_graph::NetworkGraph;

use super::plot::Plot;
use super::{graph_visual::GraphVisual, plot::Axis};
use super::{opencolor, plot::Plot};

pub struct MainLayout {
graph_visual: Option<GraphVisual>,
Expand Down Expand Up @@ -56,21 +56,21 @@ impl MainLayout {
ctx,
&[na::Point2::new(550.0, 0.0), na::Point2::new(550.0, 600.0)],
3.0,
graphics::Color::from_rgba(0, 0, 0, 50),
opencolor::with_alpha(*opencolor::BLACK, 0.2),
)?;
graphics::draw(ctx, &vertical, (na::Point2::new(0.0, 0.0),))?;

let horizontal = graphics::Mesh::new_line(
ctx,
&[na::Point2::new(550.0, 300.0), na::Point2::new(950.0, 300.0)],
3.0,
graphics::Color::from_rgba(0, 0, 0, 50),
opencolor::with_alpha(*opencolor::BLACK, 0.2),
)?;
graphics::draw(ctx, &horizontal, (na::Point2::new(0.0, 0.0),))
}

pub fn draw(&mut self, ctx: &mut ggez::Context) -> ggez::GameResult {
graphics::clear(ctx, graphics::Color::from_rgb(248, 249, 250));
graphics::clear(ctx, *opencolor::GRAY0);

if let Some(graph) = &self.graph_visual {
graph.draw(ctx)?;
Expand All @@ -79,7 +79,7 @@ impl MainLayout {
self.fitness_plot
.draw_plane(ctx, |x| format!("{}", x), |y| format!("{:.1}", y))?;
self.fitness_plot
.draw_line(ctx, &self.fitness_points, graphics::BLACK)?;
.draw_line(ctx, &self.fitness_points, *opencolor::INDIGO5)?;

self.draw_separator(ctx)
}
Expand Down
1 change: 1 addition & 0 deletions examples/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs;
pub mod cli;
pub mod graph_visual;
pub mod main_layout;
pub mod opencolor;
pub mod plot;
pub mod text;

Expand Down
175 changes: 175 additions & 0 deletions examples/helper/opencolor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use lazy_static::lazy_static;

use ggez::graphics::Color;

macro_rules! hex {
($code: expr) => {
Color::from_rgb_u32($code)
};
}

lazy_static! {
pub static ref WHITE: Color = hex!(0xffffff);
pub static ref BLACK: Color = hex!(0x000000);

// GRAY
pub static ref GRAY0: Color = hex!(0xf8f9fa);
pub static ref GRAY1: Color = hex!(0xf1f3f5);
pub static ref GRAY2: Color = hex!(0xe9ecef);
pub static ref GRAY3: Color = hex!(0xdee2e6);
pub static ref GRAY4: Color = hex!(0xced4da);
pub static ref GRAY5: Color = hex!(0xadb5bd);
pub static ref GRAY6: Color = hex!(0x868e96);
pub static ref GRAY7: Color = hex!(0x495057);
pub static ref GRAY8: Color = hex!(0x343a40);
pub static ref GRAY9: Color = hex!(0x212529);

// RED
pub static ref RED0: Color = hex!(0xfff5f5);
pub static ref RED1: Color = hex!(0xffe3e3);
pub static ref RED2: Color = hex!(0xffc9c9);
pub static ref RED3: Color = hex!(0xffa8a8);
pub static ref RED4: Color = hex!(0xff8787);
pub static ref RED5: Color = hex!(0xff6b6b);
pub static ref RED6: Color = hex!(0xfa5252);
pub static ref RED7: Color = hex!(0xf03e3e);
pub static ref RED8: Color = hex!(0xe03131);
pub static ref RED9: Color = hex!(0xc92a2a);

// PINK
pub static ref PINK0: Color = hex!(0xfff0f6);
pub static ref PINK1: Color = hex!(0xffdeeb);
pub static ref PINK2: Color = hex!(0xfcc2d7);
pub static ref PINK3: Color = hex!(0xfaa2c1);
pub static ref PINK4: Color = hex!(0xf783ac);
pub static ref PINK5: Color = hex!(0xf06595);
pub static ref PINK6: Color = hex!(0xe64980);
pub static ref PINK7: Color = hex!(0xd6336c);
pub static ref PINK8: Color = hex!(0xc2255c);
pub static ref PINK9: Color = hex!(0xa61e4d);

// GRAPE
pub static ref GRAPE0: Color = hex!(0xf8f0fc);
pub static ref GRAPE1: Color = hex!(0xf3d9fa);
pub static ref GRAPE2: Color = hex!(0xeebefa);
pub static ref GRAPE3: Color = hex!(0xe599f7);
pub static ref GRAPE4: Color = hex!(0xda77f2);
pub static ref GRAPE5: Color = hex!(0xcc5de8);
pub static ref GRAPE6: Color = hex!(0xbe4bdb);
pub static ref GRAPE7: Color = hex!(0xae3ec9);
pub static ref GRAPE8: Color = hex!(0x9c36b5);
pub static ref GRAPE9: Color = hex!(0x862e9c);

// VIOLET
pub static ref VIOLET0: Color = hex!(0xf3f0ff);
pub static ref VIOLET1: Color = hex!(0xe5dbff);
pub static ref VIOLET2: Color = hex!(0xd0bfff);
pub static ref VIOLET3: Color = hex!(0xb197fc);
pub static ref VIOLET4: Color = hex!(0x9775fa);
pub static ref VIOLET5: Color = hex!(0x845ef7);
pub static ref VIOLET6: Color = hex!(0x7950f2);
pub static ref VIOLET7: Color = hex!(0x7048e8);
pub static ref VIOLET8: Color = hex!(0x6741d9);
pub static ref VIOLET9: Color = hex!(0x5f3dc4);

// INDIGO
pub static ref INDIGO0: Color = hex!(0xedf2ff);
pub static ref INDIGO1: Color = hex!(0xdbe4ff);
pub static ref INDIGO2: Color = hex!(0xbac8ff);
pub static ref INDIGO3: Color = hex!(0x91a7ff);
pub static ref INDIGO4: Color = hex!(0x748ffc);
pub static ref INDIGO5: Color = hex!(0x5c7cfa);
pub static ref INDIGO6: Color = hex!(0x4c6ef5);
pub static ref INDIGO7: Color = hex!(0x4263eb);
pub static ref INDIGO8: Color = hex!(0x3b5bdb);
pub static ref INDIGO9: Color = hex!(0x364fc7);

// BLUE
pub static ref BLUE0: Color = hex!(0xe7f5ff);
pub static ref BLUE1: Color = hex!(0xd0ebff);
pub static ref BLUE2: Color = hex!(0xa5d8ff);
pub static ref BLUE3: Color = hex!(0x74c0fc);
pub static ref BLUE4: Color = hex!(0x4dabf7);
pub static ref BLUE5: Color = hex!(0x339af0);
pub static ref BLUE6: Color = hex!(0x228be6);
pub static ref BLUE7: Color = hex!(0x1c7ed6);
pub static ref BLUE8: Color = hex!(0x1971c2);
pub static ref BLUE9: Color = hex!(0x1864ab);

// CYAN
pub static ref CYAN0: Color = hex!(0xe3fafc);
pub static ref CYAN1: Color = hex!(0xc5f6fa);
pub static ref CYAN2: Color = hex!(0x99e9f2);
pub static ref CYAN3: Color = hex!(0x66d9e8);
pub static ref CYAN4: Color = hex!(0x3bc9db);
pub static ref CYAN5: Color = hex!(0x22b8cf);
pub static ref CYAN6: Color = hex!(0x15aabf);
pub static ref CYAN7: Color = hex!(0x1098ad);
pub static ref CYAN8: Color = hex!(0x0c8599);
pub static ref CYAN9: Color = hex!(0x0b7285);

// TEAL
pub static ref TEAL0: Color = hex!(0xe6fcf5);
pub static ref TEAL1: Color = hex!(0xc3fae8);
pub static ref TEAL2: Color = hex!(0x96f2d7);
pub static ref TEAL3: Color = hex!(0x63e6be);
pub static ref TEAL4: Color = hex!(0x38d9a9);
pub static ref TEAL5: Color = hex!(0x20c997);
pub static ref TEAL6: Color = hex!(0x12b886);
pub static ref TEAL7: Color = hex!(0x0ca678);
pub static ref TEAL8: Color = hex!(0x099268);
pub static ref TEAL9: Color = hex!(0x087f5b);

// GREEN
pub static ref GREEN0: Color = hex!(0xebfbee);
pub static ref GREEN1: Color = hex!(0xd3f9d8);
pub static ref GREEN2: Color = hex!(0xb2f2bb);
pub static ref GREEN3: Color = hex!(0x8ce99a);
pub static ref GREEN4: Color = hex!(0x69db7c);
pub static ref GREEN5: Color = hex!(0x51cf66);
pub static ref GREEN6: Color = hex!(0x40c057);
pub static ref GREEN7: Color = hex!(0x37b24d);
pub static ref GREEN8: Color = hex!(0x2f9e44);
pub static ref GREEN9: Color = hex!(0x2b8a3e);

// LIME
pub static ref LIME0: Color = hex!(0xf4fce3);
pub static ref LIME1: Color = hex!(0xe9fac8);
pub static ref LIME2: Color = hex!(0xd8f5a2);
pub static ref LIME3: Color = hex!(0xc0eb75);
pub static ref LIME4: Color = hex!(0xa9e34b);
pub static ref LIME5: Color = hex!(0x94d82d);
pub static ref LIME6: Color = hex!(0x82c91e);
pub static ref LIME7: Color = hex!(0x74b816);
pub static ref LIME8: Color = hex!(0x66a80f);
pub static ref LIME9: Color = hex!(0x5c940d);

// YELLOW
pub static ref YELLOW0: Color = hex!(0xfff9db);
pub static ref YELLOW1: Color = hex!(0xfff3bf);
pub static ref YELLOW2: Color = hex!(0xffec99);
pub static ref YELLOW3: Color = hex!(0xffe066);
pub static ref YELLOW4: Color = hex!(0xffd43b);
pub static ref YELLOW5: Color = hex!(0xfcc419);
pub static ref YELLOW6: Color = hex!(0xfab005);
pub static ref YELLOW7: Color = hex!(0xf59f00);
pub static ref YELLOW8: Color = hex!(0xf08c00);
pub static ref YELLOW9: Color = hex!(0xe67700);

// ORANGE
pub static ref ORANGE0: Color = hex!(0xfff4e6);
pub static ref ORANGE1: Color = hex!(0xffe8cc);
pub static ref ORANGE2: Color = hex!(0xffd8a8);
pub static ref ORANGE3: Color = hex!(0xffc078);
pub static ref ORANGE4: Color = hex!(0xffa94d);
pub static ref ORANGE5: Color = hex!(0xff922b);
pub static ref ORANGE6: Color = hex!(0xfd7e14);
pub static ref ORANGE7: Color = hex!(0xf76707);
pub static ref ORANGE8: Color = hex!(0xe8590c);
pub static ref ORANGE9: Color = hex!(0xd9480f);
}

pub fn with_alpha(mut color: Color, opacity: f32) -> Color {
color.a = opacity;
color
}
Loading

0 comments on commit 9e73fd2

Please sign in to comment.