From 9d32ef8b83a9e5415d55d10b2d33c0ca6824de9f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 22 Dec 2018 12:29:34 -0800 Subject: [PATCH] use const functions to construct constant colors in many cases, now that rust 2018 is here --- .../src/plugins/view/neighborhood_summary.rs | 6 ++-- editor/src/render/parcel.rs | 29 +++++++++---------- ezgui/src/color.rs | 29 ++++++++++--------- halloween/src/render.rs | 8 ++--- playground_gui/src/common.rs | 16 +++++----- playground_gui/src/debug_intersection.rs | 4 +-- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/editor/src/plugins/view/neighborhood_summary.rs b/editor/src/plugins/view/neighborhood_summary.rs index 3e8f92d7e5..709757f8f1 100644 --- a/editor/src/plugins/view/neighborhood_summary.rs +++ b/editor/src/plugins/view/neighborhood_summary.rs @@ -148,7 +148,7 @@ impl Region { const COLORS: [Color; 3] = [ // TODO these are awful choices - Color([1.0, 0.0, 0.0, 0.8]), - Color([0.0, 1.0, 0.0, 0.8]), - Color([0.0, 0.0, 1.0, 0.8]), + Color::RED.alpha(0.8), + Color::GREEN.alpha(0.8), + Color::BLUE.alpha(0.8), ]; diff --git a/editor/src/render/parcel.rs b/editor/src/render/parcel.rs index 8a5bca6c38..048e109e6e 100644 --- a/editor/src/render/parcel.rs +++ b/editor/src/render/parcel.rs @@ -8,21 +8,20 @@ use map_model::{Parcel, ParcelID}; const COLORS: [Color; 14] = [ // TODO these are awful choices - // TODO can we express these with the nicer functions? probably need constexpr - Color([1.0, 1.0, 0.0, 1.0]), - Color([1.0, 0.0, 1.0, 1.0]), - Color([0.0, 1.0, 1.0, 1.0]), - Color([0.5, 0.2, 0.7, 1.0]), - Color([0.5, 0.5, 0.0, 0.5]), - Color([0.5, 0.0, 0.5, 0.5]), - Color([0.0, 0.5, 0.5, 0.5]), - Color([0.0, 0.0, 0.5, 0.5]), - Color([0.3, 0.2, 0.5, 0.5]), - Color([0.4, 0.2, 0.5, 0.5]), - Color([0.5, 0.2, 0.5, 0.5]), - Color([0.6, 0.2, 0.5, 0.5]), - Color([0.7, 0.2, 0.5, 0.5]), - Color([0.8, 0.2, 0.5, 0.5]), + Color::rgba_f(1.0, 1.0, 0.0, 1.0), + Color::rgba_f(1.0, 0.0, 1.0, 1.0), + Color::rgba_f(0.0, 1.0, 1.0, 1.0), + Color::rgba_f(0.5, 0.2, 0.7, 1.0), + Color::rgba_f(0.5, 0.5, 0.0, 0.5), + Color::rgba_f(0.5, 0.0, 0.5, 0.5), + Color::rgba_f(0.0, 0.5, 0.5, 0.5), + Color::rgba_f(0.0, 0.0, 0.5, 0.5), + Color::rgba_f(0.3, 0.2, 0.5, 0.5), + Color::rgba_f(0.4, 0.2, 0.5, 0.5), + Color::rgba_f(0.5, 0.2, 0.5, 0.5), + Color::rgba_f(0.6, 0.2, 0.5, 0.5), + Color::rgba_f(0.7, 0.2, 0.5, 0.5), + Color::rgba_f(0.8, 0.2, 0.5, 0.5), ]; #[derive(Debug)] diff --git a/ezgui/src/color.rs b/ezgui/src/color.rs index 21d8d2231d..3f93a21094 100644 --- a/ezgui/src/color.rs +++ b/ezgui/src/color.rs @@ -3,9 +3,8 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; // Copy could be reconsidered, but eh -// TODO only pub so we can construct constants elsewhere. need const fn. #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] -pub struct Color(pub [f32; 4]); +pub struct Color(pub(crate) [f32; 4]); impl fmt::Display for Color { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -18,23 +17,25 @@ impl fmt::Display for Color { } impl Color { - pub const BLACK: Color = Color([0.0, 0.0, 0.0, 1.0]); - pub const WHITE: Color = Color([1.0, 1.0, 1.0, 1.0]); - pub const RED: Color = Color([1.0, 0.0, 0.0, 1.0]); - pub const GREEN: Color = Color([0.0, 1.0, 0.0, 1.0]); - pub const BLUE: Color = Color([0.0, 0.0, 1.0, 1.0]); - pub const CYAN: Color = Color([0.0, 1.0, 1.0, 1.0]); - pub const YELLOW: Color = Color([1.0, 1.0, 0.0, 1.0]); - pub const PURPLE: Color = Color([0.5, 0.0, 0.5, 1.0]); - pub const PINK: Color = Color([1.0, 0.41, 0.71, 1.0]); + pub const BLACK: Color = Color::rgb_f(0.0, 0.0, 0.0); + pub const WHITE: Color = Color::rgb_f(1.0, 1.0, 1.0); + pub const RED: Color = Color::rgb_f(1.0, 0.0, 0.0); + pub const GREEN: Color = Color::rgb_f(0.0, 1.0, 0.0); + pub const BLUE: Color = Color::rgb_f(0.0, 0.0, 1.0); + pub const CYAN: Color = Color::rgb_f(0.0, 1.0, 1.0); + pub const YELLOW: Color = Color::rgb_f(1.0, 1.0, 0.0); + pub const PURPLE: Color = Color::rgb_f(0.5, 0.0, 0.5); + pub const PINK: Color = Color::rgb_f(1.0, 0.41, 0.71); // TODO should assert stuff about the inputs + // TODO Once f32 can be used in const fn, make these const fn too and clean up call sites + // dividing by 255.0 pub fn rgb(r: usize, g: usize, b: usize) -> Color { Color::rgba(r, g, b, 1.0) } - pub fn rgb_f(r: f32, g: f32, b: f32) -> Color { + pub const fn rgb_f(r: f32, g: f32, b: f32) -> Color { Color([r, g, b, 1.0]) } @@ -47,7 +48,7 @@ impl Color { ]) } - pub fn rgba_f(r: f32, g: f32, b: f32, a: f32) -> Color { + pub const fn rgba_f(r: f32, g: f32, b: f32, a: f32) -> Color { Color([r, g, b, a]) } @@ -75,7 +76,7 @@ impl Color { Color([new_color.red, new_color.green, new_color.blue, 1.0]) } - pub fn alpha(&self, a: f32) -> Color { + pub const fn alpha(&self, a: f32) -> Color { Color([self.0[0], self.0[1], self.0[2], a]) } } diff --git a/halloween/src/render.rs b/halloween/src/render.rs index 06834d190b..e383105bb2 100644 --- a/halloween/src/render.rs +++ b/halloween/src/render.rs @@ -4,13 +4,13 @@ use geom::{Bounds, Line, Polygon, Pt2D}; use map_model::{Building, BuildingID, Map, Road, RoadID, LANE_THICKNESS}; // black -const BACKGROUND: Color = Color([0.0, 0.0, 0.0, 1.0]); +const BACKGROUND: Color = Color::rgb_f(0.0, 0.0, 0.0); // light orange -const ROAD: Color = Color([1.0, 154.0 / 255.0, 0.0, 1.0]); +const ROAD: Color = Color::rgb_f(1.0, 154.0 / 255.0, 0.0); // purple -const BUILDING: Color = Color([136.0 / 255.0, 30.0 / 255.0, 228.0 / 255.0, 1.0]); +const BUILDING: Color = Color::rgb_f(136.0 / 255.0, 30.0 / 255.0, 228.0 / 255.0); // dark orange / red -const PATH: Color = Color([247.0 / 255.0, 95.0 / 255.0, 28.0 / 255.0, 1.0]); +const PATH: Color = Color::rgb_f(247.0 / 255.0, 95.0 / 255.0, 28.0 / 255.0); const LINE_WIDTH: f64 = 1.0; diff --git a/playground_gui/src/common.rs b/playground_gui/src/common.rs index 527abb9b2a..26a9332758 100644 --- a/playground_gui/src/common.rs +++ b/playground_gui/src/common.rs @@ -1,13 +1,15 @@ use ezgui::{Color, GfxCtx}; use geom::{Circle, PolyLine}; -pub const WHITE: Color = Color([1.0; 4]); -pub const RED: Color = Color([1.0, 0.0, 0.0, 0.8]); -pub const GREEN: Color = Color([0.0, 1.0, 0.0, 0.8]); -pub const BLUE: Color = Color([0.0, 0.0, 1.0, 0.8]); -pub const BLACK: Color = Color([0.0, 0.0, 0.0, 0.3]); -pub const SOLID_BLACK: Color = Color([0.0, 0.0, 0.0, 0.9]); -pub const YELLOW: Color = Color([1.0, 1.0, 0.0, 0.8]); +// TODO Don't just use ezgui constants in this crate, since we want the slight transparency by +// default. +pub const WHITE: Color = Color::WHITE; +pub const RED: Color = Color::RED.alpha(0.8); +pub const GREEN: Color = Color::GREEN.alpha(0.8); +pub const BLUE: Color = Color::BLUE.alpha(0.8); +pub const BLACK: Color = Color::BLACK.alpha(0.3); +pub const SOLID_BLACK: Color = Color::BLACK.alpha(0.9); +pub const YELLOW: Color = Color::YELLOW.alpha(0.8); pub fn draw_polyline(g: &mut GfxCtx, pl: &PolyLine, thickness: f64, color: Color) { for l in pl.lines() { diff --git a/playground_gui/src/debug_intersection.rs b/playground_gui/src/debug_intersection.rs index b02058bde4..2004714a95 100644 --- a/playground_gui/src/debug_intersection.rs +++ b/playground_gui/src/debug_intersection.rs @@ -67,12 +67,12 @@ impl Iterator for RelatedColors { fn next(&mut self) -> Option { self.count -= 2; let multiplier = 0.1 * (self.count as f32); - Some(Color([ + Some(Color::rgba_f( self.r * multiplier, self.g * multiplier, self.b * multiplier, 0.8, - ])) + )) } }