|
@@ -0,0 +1,453 @@
|
|
|
+use std::{num::ParseFloatError, str::FromStr};
|
|
|
+
|
|
|
+use tui::style::{Color, Modifier, Style};
|
|
|
+
|
|
|
+use crate::RenderingMode;
|
|
|
+
|
|
|
+#[derive(Clone, Copy, Debug)]
|
|
|
+pub struct RinkColor {
|
|
|
+ pub color: Color,
|
|
|
+ pub alpha: f32,
|
|
|
+}
|
|
|
+
|
|
|
+impl Default for RinkColor {
|
|
|
+ fn default() -> Self {
|
|
|
+ Self {
|
|
|
+ color: Color::Black,
|
|
|
+ alpha: 0.0,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl RinkColor {
|
|
|
+ pub fn blend(self, other: Color) -> Color {
|
|
|
+ if self.color == Color::Reset {
|
|
|
+ Color::Reset
|
|
|
+ } else {
|
|
|
+ if self.alpha == 0.0 {
|
|
|
+ other
|
|
|
+ } else if other == Color::Reset {
|
|
|
+ self.color
|
|
|
+ } else {
|
|
|
+ let [sr, sg, sb] = to_rgb(self.color);
|
|
|
+ let [or, og, ob] = to_rgb(other);
|
|
|
+ let (sr, sg, sb, sa) = (
|
|
|
+ sr as f32 / 255.0,
|
|
|
+ sg as f32 / 255.0,
|
|
|
+ sb as f32 / 255.0,
|
|
|
+ self.alpha,
|
|
|
+ );
|
|
|
+ let (or, og, ob) = (or as f32 / 255.0, og as f32 / 255.0, ob as f32 / 255.0);
|
|
|
+ let c = Color::Rgb(
|
|
|
+ (255.0 * (sr * sa + or * (1.0 - sa))) as u8,
|
|
|
+ (255.0 * (sg * sa + og * (1.0 - sa))) as u8,
|
|
|
+ (255.0 * (sb * sa + ob * (1.0 - sa))) as u8,
|
|
|
+ );
|
|
|
+ c
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn parse_value(
|
|
|
+ v: &str,
|
|
|
+ current_max_output: f32,
|
|
|
+ required_max_output: f32,
|
|
|
+) -> Result<f32, ParseFloatError> {
|
|
|
+ if v.ends_with('%') {
|
|
|
+ Ok((v[..v.len() - 1].trim().parse::<f32>()? / 100.0) * required_max_output)
|
|
|
+ } else {
|
|
|
+ Ok((v.trim().parse::<f32>()? / current_max_output) * required_max_output)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub struct ParseColorError;
|
|
|
+
|
|
|
+fn parse_hex(color: &str) -> Result<Color, ParseColorError> {
|
|
|
+ let mut values = [0, 0, 0];
|
|
|
+ let mut color_ok = true;
|
|
|
+ for i in 0..values.len() {
|
|
|
+ if let Ok(v) = u8::from_str_radix(&color[(1 + 2 * i)..(1 + 2 * (i + 1))], 16) {
|
|
|
+ values[i] = v;
|
|
|
+ } else {
|
|
|
+ color_ok = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if color_ok {
|
|
|
+ Ok(Color::Rgb(values[0], values[1], values[2]))
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn parse_rgb(color: &str) -> Result<Color, ParseColorError> {
|
|
|
+ let mut values = [0, 0, 0];
|
|
|
+ let mut color_ok = true;
|
|
|
+ for (v, i) in color.split(',').zip(0..values.len()) {
|
|
|
+ if let Ok(v) = parse_value(v.trim(), 255.0, 255.0) {
|
|
|
+ values[i] = v as u8;
|
|
|
+ } else {
|
|
|
+ color_ok = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if color_ok {
|
|
|
+ Ok(Color::Rgb(values[0], values[1], values[2]))
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn parse_hsl(color: &str) -> Result<Color, ParseColorError> {
|
|
|
+ let mut values = [0.0, 0.0, 0.0];
|
|
|
+ let mut color_ok = true;
|
|
|
+ for (v, i) in color.split(',').zip(0..values.len()) {
|
|
|
+ if let Ok(v) = parse_value(v.trim(), if i == 0 { 360.0 } else { 100.0 }, 1.0) {
|
|
|
+ values[i] = v;
|
|
|
+ } else {
|
|
|
+ color_ok = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if color_ok {
|
|
|
+ let [h, s, l] = values;
|
|
|
+ let rgb = if s == 0.0 {
|
|
|
+ [l as u8; 3]
|
|
|
+ } else {
|
|
|
+ fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 {
|
|
|
+ if t < 0.0 {
|
|
|
+ t += 1.0;
|
|
|
+ }
|
|
|
+ if t > 1.0 {
|
|
|
+ t -= 1.0;
|
|
|
+ }
|
|
|
+ if t < 1.0 / 6.0 {
|
|
|
+ p + (q - p) * 6.0 * t
|
|
|
+ } else if t < 1.0 / 2.0 {
|
|
|
+ q
|
|
|
+ } else if t < 2.0 / 3.0 {
|
|
|
+ p + (q - p) * (2.0 / 3.0 - t) * 6.0
|
|
|
+ } else {
|
|
|
+ p
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let q = if l < 0.5 {
|
|
|
+ l * (1.0 + s)
|
|
|
+ } else {
|
|
|
+ l + s - l * s
|
|
|
+ };
|
|
|
+ let p = 2.0 * l - q;
|
|
|
+ [
|
|
|
+ (hue_to_rgb(p, q, h + 1.0 / 3.0) * 255.0) as u8,
|
|
|
+ (hue_to_rgb(p, q, h) * 255.0) as u8,
|
|
|
+ (hue_to_rgb(p, q, h - 1.0 / 3.0) * 255.0) as u8,
|
|
|
+ ]
|
|
|
+ };
|
|
|
+
|
|
|
+ Ok(Color::Rgb(rgb[0], rgb[1], rgb[2]))
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl FromStr for RinkColor {
|
|
|
+ type Err = ParseColorError;
|
|
|
+
|
|
|
+ fn from_str(color: &str) -> Result<Self, Self::Err> {
|
|
|
+ match color {
|
|
|
+ "red" => Ok(RinkColor {
|
|
|
+ color: Color::Red,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "black" => Ok(RinkColor {
|
|
|
+ color: Color::Black,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "green" => Ok(RinkColor {
|
|
|
+ color: Color::Green,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "yellow" => Ok(RinkColor {
|
|
|
+ color: Color::Yellow,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "blue" => Ok(RinkColor {
|
|
|
+ color: Color::Blue,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "magenta" => Ok(RinkColor {
|
|
|
+ color: Color::Magenta,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "cyan" => Ok(RinkColor {
|
|
|
+ color: Color::Cyan,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "gray" => Ok(RinkColor {
|
|
|
+ color: Color::Gray,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "darkgray" => Ok(RinkColor {
|
|
|
+ color: Color::DarkGray,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ // light red does not exist
|
|
|
+ "orangered" => Ok(RinkColor {
|
|
|
+ color: Color::LightRed,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "lightgreen" => Ok(RinkColor {
|
|
|
+ color: Color::LightGreen,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "lightyellow" => Ok(RinkColor {
|
|
|
+ color: Color::LightYellow,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "lightblue" => Ok(RinkColor {
|
|
|
+ color: Color::LightBlue,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ // light magenta does not exist
|
|
|
+ "orchid" => Ok(RinkColor {
|
|
|
+ color: Color::LightMagenta,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "lightcyan" => Ok(RinkColor {
|
|
|
+ color: Color::LightCyan,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ "white" => Ok(RinkColor {
|
|
|
+ color: Color::White,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ _ => {
|
|
|
+ if color.len() == 7 && color.starts_with('#') {
|
|
|
+ parse_hex(color).map(|c| RinkColor {
|
|
|
+ color: c,
|
|
|
+ alpha: 1.0,
|
|
|
+ })
|
|
|
+ } else if color.starts_with("rgb(") {
|
|
|
+ let color_values = color[4..].trim_end_matches(')');
|
|
|
+ if color.matches(',').count() == 4 {
|
|
|
+ let (alpha, rgb_values) =
|
|
|
+ color_values.rsplit_once(',').ok_or(ParseColorError)?;
|
|
|
+ if let Ok(a) = alpha.parse() {
|
|
|
+ parse_rgb(rgb_values).map(|c| RinkColor { color: c, alpha: a })
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parse_rgb(color_values).map(|c| RinkColor {
|
|
|
+ color: c,
|
|
|
+ alpha: 1.0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else if color.starts_with("rgba(") {
|
|
|
+ let color_values = color[5..].trim_end_matches(')');
|
|
|
+ if color.matches(',').count() == 3 {
|
|
|
+ let (rgb_values, alpha) =
|
|
|
+ color_values.rsplit_once(',').ok_or(ParseColorError)?;
|
|
|
+ if let Ok(a) = parse_value(alpha, 1.0, 1.0) {
|
|
|
+ parse_rgb(rgb_values).map(|c| RinkColor { color: c, alpha: a })
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parse_rgb(color_values).map(|c| RinkColor {
|
|
|
+ color: c,
|
|
|
+ alpha: 1.0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else if color.starts_with("hsl(") {
|
|
|
+ let color_values = color[4..].trim_end_matches(')');
|
|
|
+ if color.matches(',').count() == 3 {
|
|
|
+ let (rgb_values, alpha) =
|
|
|
+ color_values.rsplit_once(',').ok_or(ParseColorError)?;
|
|
|
+ if let Ok(a) = parse_value(alpha, 1.0, 1.0) {
|
|
|
+ parse_hsl(rgb_values).map(|c| RinkColor { color: c, alpha: a })
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parse_hsl(color_values).map(|c| RinkColor {
|
|
|
+ color: c,
|
|
|
+ alpha: 1.0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else if color.starts_with("hsla(") {
|
|
|
+ let color_values = color[5..].trim_end_matches(')');
|
|
|
+ if color.matches(',').count() == 3 {
|
|
|
+ let (rgb_values, alpha) =
|
|
|
+ color_values.rsplit_once(',').ok_or(ParseColorError)?;
|
|
|
+ if let Ok(a) = parse_value(alpha, 1.0, 1.0) {
|
|
|
+ parse_hsl(rgb_values).map(|c| RinkColor { color: c, alpha: a })
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parse_hsl(color_values).map(|c| RinkColor {
|
|
|
+ color: c,
|
|
|
+ alpha: 1.0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Err(ParseColorError)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn to_rgb(c: Color) -> [u8; 3] {
|
|
|
+ match c {
|
|
|
+ Color::Black => [0, 0, 0],
|
|
|
+ Color::Red => [255, 0, 0],
|
|
|
+ Color::Green => [0, 128, 0],
|
|
|
+ Color::Yellow => [255, 255, 0],
|
|
|
+ Color::Blue => [0, 0, 255],
|
|
|
+ Color::Magenta => [255, 0, 255],
|
|
|
+ Color::Cyan => [0, 255, 255],
|
|
|
+ Color::Gray => [128, 128, 128],
|
|
|
+ Color::DarkGray => [169, 169, 169],
|
|
|
+ Color::LightRed => [255, 69, 0],
|
|
|
+ Color::LightGreen => [144, 238, 144],
|
|
|
+ Color::LightYellow => [255, 255, 224],
|
|
|
+ Color::LightBlue => [173, 216, 230],
|
|
|
+ Color::LightMagenta => [218, 112, 214],
|
|
|
+ Color::LightCyan => [224, 255, 255],
|
|
|
+ Color::White => [255, 255, 255],
|
|
|
+ Color::Rgb(r, g, b) => [r, g, b],
|
|
|
+ Color::Indexed(idx) => match idx {
|
|
|
+ 16..=231 => {
|
|
|
+ let v = idx - 16;
|
|
|
+ // add 3 to round up
|
|
|
+ let r = ((v as u16 / 36) * 255 + 3) / 6;
|
|
|
+ let g = (((v as u16 % 36) / 6) * 255 + 3) / 6;
|
|
|
+ let b = ((v as u16 % 6) * 255 + 3) / 6;
|
|
|
+ let vals = [v / 36, (v % 36) / 6, v % 6];
|
|
|
+ [r as u8, g as u8, b as u8]
|
|
|
+ }
|
|
|
+ 232..=255 => {
|
|
|
+ let l = (idx - 232) / 24;
|
|
|
+ [l; 3]
|
|
|
+ }
|
|
|
+ // rink will never generate these colors, but they might be on the screen from another program
|
|
|
+ _ => [0, 0, 0],
|
|
|
+ },
|
|
|
+ _ => todo!("{c:?}"),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn convert(mode: RenderingMode, c: Color) -> Color {
|
|
|
+ if let Color::Reset = c {
|
|
|
+ c
|
|
|
+ } else {
|
|
|
+ match mode {
|
|
|
+ crate::RenderingMode::BaseColors => match c {
|
|
|
+ Color::Rgb(_, _, _) => panic!("cannot convert rgb color to base color"),
|
|
|
+ Color::Indexed(_) => panic!("cannot convert Ansi color to base color"),
|
|
|
+ _ => c,
|
|
|
+ },
|
|
|
+ crate::RenderingMode::Rgb => {
|
|
|
+ let rgb = to_rgb(c);
|
|
|
+ Color::Rgb(rgb[0], rgb[1], rgb[2])
|
|
|
+ }
|
|
|
+ crate::RenderingMode::Ansi => match c {
|
|
|
+ Color::Indexed(_) => c,
|
|
|
+ _ => {
|
|
|
+ let rgb = to_rgb(c);
|
|
|
+ // 16-231: 6 × 6 × 6 color cube
|
|
|
+ // 232-255: 24 step grayscale
|
|
|
+ if rgb[0] == rgb[1] && rgb[1] == rgb[2] {
|
|
|
+ let idx = 232 + (rgb[0] as u16 * 23 / 255) as u8;
|
|
|
+ Color::Indexed(idx)
|
|
|
+ } else {
|
|
|
+ let r = (rgb[0] as u16 * 6) / 255;
|
|
|
+ let g = (rgb[1] as u16 * 6) / 255;
|
|
|
+ let b = (rgb[2] as u16 * 6) / 255;
|
|
|
+ let idx = 16 + r * 36 + g * 6 + b;
|
|
|
+ Color::Indexed(idx as u8)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn rgb_to_ansi() {
|
|
|
+ for idx in 16..=231 {
|
|
|
+ let idxed = Color::Indexed(idx);
|
|
|
+ let rgb = to_rgb(idxed);
|
|
|
+ // gray scale colors have two equivelent repersentations
|
|
|
+ let color = Color::Rgb(rgb[0], rgb[1], rgb[2]);
|
|
|
+ let converted = convert(RenderingMode::Ansi, color);
|
|
|
+ if let Color::Indexed(i) = converted {
|
|
|
+ if rgb[0] != rgb[1] || rgb[1] != rgb[2] {
|
|
|
+ assert_eq!(idxed, converted);
|
|
|
+ } else {
|
|
|
+ assert!(i >= 232 && i <= 255);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ panic!("color is not indexed")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for idx in 232..=255 {
|
|
|
+ let idxed = Color::Indexed(idx);
|
|
|
+ let rgb = to_rgb(idxed);
|
|
|
+ assert!(rgb[0] == rgb[1] && rgb[1] == rgb[2]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Clone, Copy)]
|
|
|
+pub struct RinkStyle {
|
|
|
+ pub fg: Option<RinkColor>,
|
|
|
+ pub bg: Option<RinkColor>,
|
|
|
+ pub add_modifier: Modifier,
|
|
|
+ pub sub_modifier: Modifier,
|
|
|
+}
|
|
|
+
|
|
|
+impl Default for RinkStyle {
|
|
|
+ fn default() -> Self {
|
|
|
+ Self {
|
|
|
+ fg: Some(RinkColor {
|
|
|
+ color: Color::White,
|
|
|
+ alpha: 1.0,
|
|
|
+ }),
|
|
|
+ bg: None,
|
|
|
+ add_modifier: Modifier::empty(),
|
|
|
+ sub_modifier: Modifier::empty(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl RinkStyle {
|
|
|
+ pub fn add_modifier(mut self, m: Modifier) -> Self {
|
|
|
+ self.sub_modifier.remove(m);
|
|
|
+ self.add_modifier.insert(m);
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn remove_modifier(mut self, m: Modifier) -> Self {
|
|
|
+ self.add_modifier.remove(m);
|
|
|
+ self.sub_modifier.insert(m);
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn merge(mut self, other: RinkStyle) -> Self {
|
|
|
+ self.fg = self.fg.or(other.fg);
|
|
|
+ self.add_modifier(other.add_modifier)
|
|
|
+ .remove_modifier(other.sub_modifier)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl Into<Style> for RinkStyle {
|
|
|
+ fn into(self) -> Style {
|
|
|
+ Style {
|
|
|
+ fg: self.fg.map(|c| c.color),
|
|
|
+ bg: self.bg.map(|c| c.color),
|
|
|
+ add_modifier: self.add_modifier,
|
|
|
+ sub_modifier: self.sub_modifier,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|