2019-11-24 18:21:27 +03:00
|
|
|
use crate::ScreenDims;
|
2019-10-24 02:30:23 +03:00
|
|
|
use geom::Angle;
|
2018-12-06 22:05:11 +03:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2018-12-03 20:49:20 +03:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-11-24 18:21:30 +03:00
|
|
|
// Group index, texture index
|
|
|
|
type TextureID = (f32, f32);
|
|
|
|
|
2018-12-03 20:49:20 +03:00
|
|
|
// Copy could be reconsidered, but eh
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
|
2019-09-11 02:41:35 +03:00
|
|
|
pub enum Color {
|
|
|
|
RGBA(f32, f32, f32, f32),
|
2019-11-24 18:21:27 +03:00
|
|
|
// Tiles seamlessly through all of map-space.
|
|
|
|
TileTexture(TextureID, ScreenDims),
|
2019-10-24 02:30:23 +03:00
|
|
|
// Stretches the entire texture to fit the entire polygon. Rotates from the center of the
|
2019-10-30 02:36:19 +03:00
|
|
|
// polygon. Not sure what this means for anything but circles right now. Have to manually
|
2019-11-24 18:21:27 +03:00
|
|
|
// fiddle with the original orientation to fix y inversion.
|
|
|
|
StretchTexture(TextureID, ScreenDims, Angle),
|
2019-10-25 00:07:03 +03:00
|
|
|
// A polygon with UV coordinates for each point must be used.
|
2019-11-24 18:21:30 +03:00
|
|
|
CustomUVTexture(TextureID),
|
2019-10-30 03:19:36 +03:00
|
|
|
// TODO Figure out how to pack more data into this.
|
|
|
|
HatchingStyle1,
|
|
|
|
HatchingStyle2,
|
2019-09-11 02:41:35 +03:00
|
|
|
}
|
2018-12-03 20:49:20 +03:00
|
|
|
|
|
|
|
impl fmt::Display for Color {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-09-11 02:41:35 +03:00
|
|
|
match self {
|
|
|
|
Color::RGBA(r, g, b, a) => write!(f, "Color(r={}, g={}, b={}, a={})", r, g, b, a),
|
2019-11-24 18:21:27 +03:00
|
|
|
Color::TileTexture(id, dims) => write!(
|
2019-11-24 18:21:30 +03:00
|
|
|
f,
|
|
|
|
"Color::TileTexture({}:{}, width={}, height={})",
|
2019-11-24 18:21:27 +03:00
|
|
|
id.0, id.1, dims.width, dims.height
|
2019-11-24 18:21:30 +03:00
|
|
|
),
|
2019-11-24 18:21:27 +03:00
|
|
|
Color::StretchTexture(id, dims, angle) => write!(
|
2019-11-24 18:21:21 +03:00
|
|
|
f,
|
|
|
|
"Color::StretchTexture({}:{}, width={}, height={}, {})",
|
2019-11-24 18:21:27 +03:00
|
|
|
id.0, id.1, dims.width, dims.height, angle
|
2019-11-24 18:21:21 +03:00
|
|
|
),
|
2019-11-24 18:21:30 +03:00
|
|
|
Color::CustomUVTexture(id) => write!(f, "Color::CustomUVTexture({}:{})", id.0, id.1),
|
2019-10-30 03:19:36 +03:00
|
|
|
Color::HatchingStyle1 => write!(f, "Color::HatchingStyle1"),
|
|
|
|
Color::HatchingStyle2 => write!(f, "Color::HatchingStyle2"),
|
2019-09-11 02:41:35 +03:00
|
|
|
}
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Color {
|
2019-12-17 02:40:32 +03:00
|
|
|
// TODO Won't this confuse the shader? :P
|
|
|
|
pub const INVISIBLE: Color = Color::rgba_f(1.0, 0.0, 0.0, 0.0);
|
2018-12-22 23:29:34 +03:00
|
|
|
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);
|
2019-02-13 00:42:52 +03:00
|
|
|
pub const ORANGE: Color = Color::rgb_f(1.0, 0.55, 0.0);
|
2018-12-03 20:49:20 +03:00
|
|
|
|
|
|
|
// TODO should assert stuff about the inputs
|
|
|
|
|
2018-12-22 23:29:34 +03:00
|
|
|
// TODO Once f32 can be used in const fn, make these const fn too and clean up call sites
|
2019-08-07 19:41:40 +03:00
|
|
|
// dividing by 255.0. https://github.com/rust-lang/rust/issues/57241
|
2018-12-03 20:49:20 +03:00
|
|
|
pub fn rgb(r: usize, g: usize, b: usize) -> Color {
|
|
|
|
Color::rgba(r, g, b, 1.0)
|
|
|
|
}
|
|
|
|
|
2018-12-22 23:29:34 +03:00
|
|
|
pub const fn rgb_f(r: f32, g: f32, b: f32) -> Color {
|
2019-09-11 02:41:35 +03:00
|
|
|
Color::RGBA(r, g, b, 1.0)
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rgba(r: usize, g: usize, b: usize, a: f32) -> Color {
|
2019-09-11 02:41:35 +03:00
|
|
|
Color::RGBA(
|
2018-12-03 20:49:20 +03:00
|
|
|
(r as f32) / 255.0,
|
|
|
|
(g as f32) / 255.0,
|
|
|
|
(b as f32) / 255.0,
|
|
|
|
a,
|
2019-09-11 02:41:35 +03:00
|
|
|
)
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 23:29:34 +03:00
|
|
|
pub const fn rgba_f(r: f32, g: f32, b: f32, a: f32) -> Color {
|
2019-09-11 02:41:35 +03:00
|
|
|
Color::RGBA(r, g, b, a)
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 23:14:51 +03:00
|
|
|
pub const fn grey(f: f32) -> Color {
|
2019-09-11 02:41:35 +03:00
|
|
|
Color::RGBA(f, f, f, 1.0)
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 02:41:35 +03:00
|
|
|
pub fn alpha(&self, a: f32) -> Color {
|
|
|
|
match self {
|
|
|
|
Color::RGBA(r, g, b, _) => Color::RGBA(*r, *g, *b, a),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|
2019-05-30 20:44:53 +03:00
|
|
|
|
2019-12-18 02:55:47 +03:00
|
|
|
pub fn fade(&self, factor: f32) -> Color {
|
|
|
|
match self {
|
|
|
|
Color::RGBA(r, g, b, a) => Color::RGBA(*r / factor, *g / factor, *b / factor, *a),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:21:23 +03:00
|
|
|
pub fn hex(raw: &str) -> Color {
|
2019-06-08 23:11:20 +03:00
|
|
|
// Skip the leading '#'
|
|
|
|
let r = usize::from_str_radix(&raw[1..3], 16).unwrap();
|
|
|
|
let g = usize::from_str_radix(&raw[3..5], 16).unwrap();
|
|
|
|
let b = usize::from_str_radix(&raw[5..7], 16).unwrap();
|
|
|
|
Color::rgb(r, g, b)
|
|
|
|
}
|
2019-10-24 02:30:23 +03:00
|
|
|
|
|
|
|
pub fn rotate(&self, angle: Angle) -> Color {
|
|
|
|
match self {
|
2019-11-24 18:21:21 +03:00
|
|
|
Color::StretchTexture(id, dims, _) => Color::StretchTexture(*id, *dims, angle),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 18:21:27 +03:00
|
|
|
pub fn texture_dims(&self) -> ScreenDims {
|
2019-11-24 18:21:21 +03:00
|
|
|
match self {
|
|
|
|
Color::TileTexture(_, dims) => *dims,
|
|
|
|
Color::StretchTexture(_, dims, _) => *dims,
|
2019-10-24 02:30:23 +03:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 20:49:20 +03:00
|
|
|
}
|