Initial implementation of rgb() function

This commit is contained in:
ConnorSkees 2020-02-08 09:37:48 -05:00
parent c5bc6062a0
commit 08ca3f6d01
4 changed files with 55 additions and 24 deletions

View File

@ -1,23 +1,20 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::convert::TryInto;
use super::Builtin; use super::Builtin;
use crate::color::Color; use crate::color::Color;
use crate::value::Value; use crate::value::Value;
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) { pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
f.insert( decl!(f "rgb", |args| {
"rgb".to_owned(),
Box::new(|args| {
let channels = args.get("channels").unwrap_or(&Value::Null); let channels = args.get("channels").unwrap_or(&Value::Null);
if channels.is_null() { if channels.is_null() {
let _red: &Value = arg!(args, 0, "red"); let red: u16 = arg!(args, 0, "red").clone().try_into().unwrap();
let _green: &Value = arg!(args, 1, "green"); let green: u16 = arg!(args, 1, "green").clone().try_into().unwrap();
let _blue: &Value = arg!(args, 2, "blue"); let blue: u16 = arg!(args, 2, "blue").clone().try_into().unwrap();
// Value::Color(Color::RGB(red, blue, green)) Some(Value::Color(Color::from_values(red, green, blue, 1)))
} else { } else {
todo!("channels variable in `rgb`") todo!("channels variable in `rgb`")
}; }
todo!() });
}),
);
} }

View File

@ -26,9 +26,9 @@ impl Color {
pub fn from_values(red: u16, green: u16, blue: u16, alpha: u16) -> Self { pub fn from_values(red: u16, green: u16, blue: u16, alpha: u16) -> Self {
let repr = if alpha >= 1 { let repr = if alpha >= 1 {
format!("#{:X}{:X}{:X}", red, green, blue) format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
} else { } else {
format!("#{:X}{:X}{:X}{:X}", red, green, blue, alpha) format!("#{:0>2x}{:0>2x}{:0>2x}{:0>2x}", red, green, blue, alpha)
}; };
Color { Color {
red, red,

View File

@ -1,8 +1,12 @@
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
use std::convert::TryInto;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::iter::{Iterator, Peekable}; use std::iter::{Iterator, Peekable};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use num_rational::BigRational;
use num_bigint::BigInt;
use crate::args::eat_call_args; use crate::args::eat_call_args;
use crate::builtin::GLOBAL_FUNCTIONS; use crate::builtin::GLOBAL_FUNCTIONS;
use crate::color::Color; use crate::color::Color;
@ -70,7 +74,7 @@ pub(crate) enum Value {
True, True,
False, False,
Null, Null,
Dimension(Dimension, Unit), Dimension(BigRational, Unit),
List(Vec<Value>, ListSeparator), List(Vec<Value>, ListSeparator),
Color(Color), Color(Color),
BinaryOp(Box<Value>, Op, Box<Value>), BinaryOp(Box<Value>, Op, Box<Value>),
@ -214,6 +218,28 @@ impl Display for Value {
} }
} }
impl TryInto<u16> for Value {
type Error = &'static str;
fn try_into(self) -> Result<u16, Self::Error> {
match self {
Self::Dimension(n, Unit::Percent) => {
todo!()
}
Self::Dimension(n, Unit::None) => {
if n >= BigRational::from_integer(BigInt::from(255)) {
Ok(255)
} else {
Ok(n.to_integer().to_str_radix(10).parse().unwrap())
}
}
Self::Dimension(n, _) => {
Err("Expected `val` to have no units or \"%\".")
}
_ => Err("expected number")
}
}
}
impl Value { impl Value {
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {
self == &Value::Null self == &Value::Null
@ -343,9 +369,7 @@ impl Value {
Unit::None Unit::None
}; };
Some(Value::Dimension( Some(Value::Dimension(
Dimension { val.parse().expect("error parsing integer"),
val: val.parse().unwrap(),
},
unit, unit,
)) ))
} }

View File

@ -27,7 +27,17 @@ test!(preserves_hex_3_val_000, "a {\n color: #000;\n}\n");
test!(preserves_hex_3_val_123, "a {\n color: #123;\n}\n"); test!(preserves_hex_3_val_123, "a {\n color: #123;\n}\n");
test!(preserves_hex_3_val_ab2, "a {\n color: #ab2;\n}\n"); test!(preserves_hex_3_val_ab2, "a {\n color: #ab2;\n}\n");
// test!( // test!(
// , // converts_rgb_to_named_color,
// "a {\n color: OrAnGe;\n}\n", // "a {\n color: rgb(0, 0, 0);\n}\n",
// "a {\n color: OrAnGe;\n}\n" // "a {\n color: black;\n}\n"
// ); // );
test!(
rgb_pads_0,
"a {\n color: rgb(1, 2, 3);\n}\n",
"a {\n color: #010203;\n}\n"
);
test!(
rgb_double_digits,
"a {\n color: rgb(255, 255, 255);\n}\n",
"a {\n color: #ffffff;\n}\n"
);