Commit 75059964 authored by Simon Goller's avatar Simon Goller
Browse files

Introduce Color::from_hex_string(&str)

parent 3f8d66e2
......@@ -77,9 +77,12 @@ pub fn draw_nodes(
rendering.stroke();
let time = clock.wall_time();
crate::console_log!("Test");
let node_color = resources::Color::with_hex_string("#6800e7").unwrap();
crate::console_log!("Node color: {:?}", node_color);
if let Some(amount_completed) = node.amount_completed(time) {
rendering.begin_path();
rendering.set_stroke_style(&resources::Color::with_rgb(0.406, 0.0, 0.902));
rendering.set_stroke_style(&node_color);
rendering.arc(
pos.0.x as f64,
pos.0.y as f64,
......@@ -93,9 +96,11 @@ pub fn draw_nodes(
if let Some(ping_time) = node.ping_time {
let radius = (time - ping_time) * 180.0 + 18.0;
let alpha = (1.0 - (time - ping_time) * 2.0).max(0.0).powi(2);
let mut ring_color = node_color.clone();
ring_color.alpha = alpha;
rendering.begin_path();
rendering.set_stroke_style(&resources::Color::with_rgba(0.406, 0.0, 0.902, alpha));
rendering.set_stroke_style(&ring_color);
rendering.arc(
pos.0.x as f64,
pos.0.y as f64,
......
......@@ -3,7 +3,22 @@ use anyhow::Context;
use wasm_bindgen::JsCast;
use web_sys;
#[derive(Copy, Clone)]
fn get_hex_color_format(value: &str) -> u32 {
let valid_characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f',
'A', 'B', 'C', 'D', 'E', 'F'];
let (hash_tag, value) = value.split_at(1);
if hash_tag == "#" {
value.chars().map(|character| if valid_characters.contains(&character) { 1 } else { 0 })
.fold(0, |acc, x| acc * x + 1)
} else {
0
}
}
#[derive(Copy, Clone, Debug)]
pub struct Color {
pub red: f64,
pub green: f64,
......@@ -28,6 +43,43 @@ impl Color {
alpha: 1.0,
}
}
pub fn with_hex_string(hex_string: &str) -> Option<Self> {
match get_hex_color_format(hex_string) {
3 => {
let number = u16::from_str_radix(hex_string.get(1..4).unwrap(), 16).unwrap() ;
let r = (number & 0xF00) >> 8;
let g = (number & 0x0F0) >> 4;
let b = number & 0x00F;
Some(Self::with_rgb(r as f64 / 15.0, g as f64 / 15.0, b as f64 / 15.0))
},
4 => {
let number = u16::from_str_radix(hex_string.get(1..5).unwrap(), 16).unwrap() ;
let r = (number & 0xF000) >> 12;
let g = (number & 0x0F00) >> 8;
let b = (number & 0x00F0) >> 4;
let a = number & 0x000F;
Some(Self::with_rgba(r as f64 / 15.0, g as f64 / 15.0, b as f64 / 15.0, a as f64 / 15.0))
},
6 => {
let number = u32::from_str_radix(hex_string.get(1..7).unwrap(), 16).unwrap();
let r = (number & 0xFF0000) >> 16;
let g = (number & 0x00FF00) >> 8;
let b = number & 0x0000FF;
Some(Self::with_rgb(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0))
},
8 => {
let number = u32::from_str_radix(hex_string.get(1..9).unwrap(), 16).unwrap();
let r = (number & 0xFF000000) >> 24;
let g = (number & 0x00FF0000) >> 16;
let b = (number & 0x0000FF00) >> 8;
let a = number & 0x000000FF;
Some(Self::with_rgba(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0, a as f64 / 255.0))
},
_ => {
None
}
}
}
pub fn as_color_string(&self) -> String {
format!(
......
......@@ -59,7 +59,7 @@ impl state::State for InGameState {
fn update(&mut self) -> state::Transition {
{
let rendering = self.resources.get::<resources::Rendering>().unwrap();
rendering.set_fill_style(&resources::Color::with_rgb(0.0625, 0.06, 0.125));
rendering.set_fill_style(&resources::Color::with_hex_string("#100e23").unwrap());
rendering.fill_rect(0.0, 0.0, 1920.0, 1080.0);
}
self.schedule.execute(&mut self.world, &mut self.resources);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment