use const functions to construct constant colors in many cases, now that rust 2018 is here

This commit is contained in:
Dustin Carlino 2018-12-22 12:29:34 -08:00
parent 0f62da0f63
commit 9d32ef8b83
6 changed files with 47 additions and 45 deletions

View File

@ -148,7 +148,7 @@ impl Region {
const COLORS: [Color; 3] = [ const COLORS: [Color; 3] = [
// TODO these are awful choices // TODO these are awful choices
Color([1.0, 0.0, 0.0, 0.8]), Color::RED.alpha(0.8),
Color([0.0, 1.0, 0.0, 0.8]), Color::GREEN.alpha(0.8),
Color([0.0, 0.0, 1.0, 0.8]), Color::BLUE.alpha(0.8),
]; ];

View File

@ -8,21 +8,20 @@ use map_model::{Parcel, ParcelID};
const COLORS: [Color; 14] = [ const COLORS: [Color; 14] = [
// TODO these are awful choices // TODO these are awful choices
// TODO can we express these with the nicer functions? probably need constexpr Color::rgba_f(1.0, 1.0, 0.0, 1.0),
Color([1.0, 1.0, 0.0, 1.0]), Color::rgba_f(1.0, 0.0, 1.0, 1.0),
Color([1.0, 0.0, 1.0, 1.0]), Color::rgba_f(0.0, 1.0, 1.0, 1.0),
Color([0.0, 1.0, 1.0, 1.0]), Color::rgba_f(0.5, 0.2, 0.7, 1.0),
Color([0.5, 0.2, 0.7, 1.0]), Color::rgba_f(0.5, 0.5, 0.0, 0.5),
Color([0.5, 0.5, 0.0, 0.5]), Color::rgba_f(0.5, 0.0, 0.5, 0.5),
Color([0.5, 0.0, 0.5, 0.5]), Color::rgba_f(0.0, 0.5, 0.5, 0.5),
Color([0.0, 0.5, 0.5, 0.5]), Color::rgba_f(0.0, 0.0, 0.5, 0.5),
Color([0.0, 0.0, 0.5, 0.5]), Color::rgba_f(0.3, 0.2, 0.5, 0.5),
Color([0.3, 0.2, 0.5, 0.5]), Color::rgba_f(0.4, 0.2, 0.5, 0.5),
Color([0.4, 0.2, 0.5, 0.5]), Color::rgba_f(0.5, 0.2, 0.5, 0.5),
Color([0.5, 0.2, 0.5, 0.5]), Color::rgba_f(0.6, 0.2, 0.5, 0.5),
Color([0.6, 0.2, 0.5, 0.5]), Color::rgba_f(0.7, 0.2, 0.5, 0.5),
Color([0.7, 0.2, 0.5, 0.5]), Color::rgba_f(0.8, 0.2, 0.5, 0.5),
Color([0.8, 0.2, 0.5, 0.5]),
]; ];
#[derive(Debug)] #[derive(Debug)]

View File

@ -3,9 +3,8 @@ use serde_derive::{Deserialize, Serialize};
use std::fmt; use std::fmt;
// Copy could be reconsidered, but eh // 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)] #[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 { impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -18,23 +17,25 @@ impl fmt::Display for Color {
} }
impl Color { impl Color {
pub const BLACK: Color = Color([0.0, 0.0, 0.0, 1.0]); pub const BLACK: Color = Color::rgb_f(0.0, 0.0, 0.0);
pub const WHITE: Color = Color([1.0, 1.0, 1.0, 1.0]); pub const WHITE: Color = Color::rgb_f(1.0, 1.0, 1.0);
pub const RED: Color = Color([1.0, 0.0, 0.0, 1.0]); pub const RED: Color = Color::rgb_f(1.0, 0.0, 0.0);
pub const GREEN: Color = Color([0.0, 1.0, 0.0, 1.0]); pub const GREEN: Color = Color::rgb_f(0.0, 1.0, 0.0);
pub const BLUE: Color = Color([0.0, 0.0, 1.0, 1.0]); pub const BLUE: Color = Color::rgb_f(0.0, 0.0, 1.0);
pub const CYAN: Color = Color([0.0, 1.0, 1.0, 1.0]); pub const CYAN: Color = Color::rgb_f(0.0, 1.0, 1.0);
pub const YELLOW: Color = Color([1.0, 1.0, 0.0, 1.0]); pub const YELLOW: Color = Color::rgb_f(1.0, 1.0, 0.0);
pub const PURPLE: Color = Color([0.5, 0.0, 0.5, 1.0]); pub const PURPLE: Color = Color::rgb_f(0.5, 0.0, 0.5);
pub const PINK: Color = Color([1.0, 0.41, 0.71, 1.0]); pub const PINK: Color = Color::rgb_f(1.0, 0.41, 0.71);
// TODO should assert stuff about the inputs // 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 { pub fn rgb(r: usize, g: usize, b: usize) -> Color {
Color::rgba(r, g, b, 1.0) 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]) 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]) Color([r, g, b, a])
} }
@ -75,7 +76,7 @@ impl Color {
Color([new_color.red, new_color.green, new_color.blue, 1.0]) 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]) Color([self.0[0], self.0[1], self.0[2], a])
} }
} }

View File

@ -4,13 +4,13 @@ use geom::{Bounds, Line, Polygon, Pt2D};
use map_model::{Building, BuildingID, Map, Road, RoadID, LANE_THICKNESS}; use map_model::{Building, BuildingID, Map, Road, RoadID, LANE_THICKNESS};
// black // 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 // 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 // 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 // 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; const LINE_WIDTH: f64 = 1.0;

View File

@ -1,13 +1,15 @@
use ezgui::{Color, GfxCtx}; use ezgui::{Color, GfxCtx};
use geom::{Circle, PolyLine}; use geom::{Circle, PolyLine};
pub const WHITE: Color = Color([1.0; 4]); // TODO Don't just use ezgui constants in this crate, since we want the slight transparency by
pub const RED: Color = Color([1.0, 0.0, 0.0, 0.8]); // default.
pub const GREEN: Color = Color([0.0, 1.0, 0.0, 0.8]); pub const WHITE: Color = Color::WHITE;
pub const BLUE: Color = Color([0.0, 0.0, 1.0, 0.8]); pub const RED: Color = Color::RED.alpha(0.8);
pub const BLACK: Color = Color([0.0, 0.0, 0.0, 0.3]); pub const GREEN: Color = Color::GREEN.alpha(0.8);
pub const SOLID_BLACK: Color = Color([0.0, 0.0, 0.0, 0.9]); pub const BLUE: Color = Color::BLUE.alpha(0.8);
pub const YELLOW: Color = Color([1.0, 1.0, 0.0, 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) { pub fn draw_polyline(g: &mut GfxCtx, pl: &PolyLine, thickness: f64, color: Color) {
for l in pl.lines() { for l in pl.lines() {

View File

@ -67,12 +67,12 @@ impl Iterator for RelatedColors {
fn next(&mut self) -> Option<Color> { fn next(&mut self) -> Option<Color> {
self.count -= 2; self.count -= 2;
let multiplier = 0.1 * (self.count as f32); let multiplier = 0.1 * (self.count as f32);
Some(Color([ Some(Color::rgba_f(
self.r * multiplier, self.r * multiplier,
self.g * multiplier, self.g * multiplier,
self.b * multiplier, self.b * multiplier,
0.8, 0.8,
])) ))
} }
} }