Properly parse floating point numbers and rgba()
This commit is contained in:
parent
2a6ec38fd4
commit
c75e5cc553
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -73,6 +73,7 @@ dependencies = [
|
|||||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num-bigint 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num-bigint 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num-rational 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num-rational 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ clap = { version = "2.33.0", optional = true }
|
|||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
num-rational = "0.2.3"
|
num-rational = "0.2.3"
|
||||||
num-bigint = "0.2.6"
|
num-bigint = "0.2.6"
|
||||||
|
num-traits = "0.2.11"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["commandline"]
|
default = ["commandline"]
|
||||||
|
@ -2,7 +2,6 @@ use std::collections::BTreeMap;
|
|||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_rational::BigRational;
|
|
||||||
|
|
||||||
use super::Builtin;
|
use super::Builtin;
|
||||||
use crate::color::Color;
|
use crate::color::Color;
|
||||||
@ -17,7 +16,23 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
let red: u16 = arg!(args, 0, "red").clone().try_into().unwrap();
|
let red: u16 = arg!(args, 0, "red").clone().try_into().unwrap();
|
||||||
let green: u16 = arg!(args, 1, "green").clone().try_into().unwrap();
|
let green: u16 = arg!(args, 1, "green").clone().try_into().unwrap();
|
||||||
let blue: u16 = arg!(args, 2, "blue").clone().try_into().unwrap();
|
let blue: u16 = arg!(args, 2, "blue").clone().try_into().unwrap();
|
||||||
Some(Value::Color(Color::from_values(red, green, blue, 1)))
|
Some(Value::Color(Color::from_values(red, green, blue, Number::from(1))))
|
||||||
|
} else {
|
||||||
|
todo!("channels variable in `rgb`")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
decl!(f "rgba", |args, _| {
|
||||||
|
let channels = args.get("channels").unwrap_or(&Value::Null);
|
||||||
|
if channels.is_null() {
|
||||||
|
let red: u16 = arg!(args, 0, "red").clone().try_into().unwrap();
|
||||||
|
let green: u16 = arg!(args, 1, "green").clone().try_into().unwrap();
|
||||||
|
let blue: u16 = arg!(args, 2, "blue").clone().try_into().unwrap();
|
||||||
|
let alpha = match arg!(args, 3, "alpha").clone().eval() {
|
||||||
|
Value::Dimension(n, Unit::None) => n,
|
||||||
|
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||||
|
_ => todo!("expected either unitless or % number for alpha"),
|
||||||
|
};
|
||||||
|
Some(Value::Color(Color::from_values(red, green, blue, alpha)))
|
||||||
} else {
|
} else {
|
||||||
todo!("channels variable in `rgb`")
|
todo!("channels variable in `rgb`")
|
||||||
}
|
}
|
||||||
@ -42,14 +57,14 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
});
|
});
|
||||||
decl!(f "opacity", |args, _| {
|
decl!(f "opacity", |args, _| {
|
||||||
match arg!(args, 0, "color") {
|
match arg!(args, 0, "color") {
|
||||||
Value::Color(c) => Some(Value::Dimension(Number::new(BigRational::new(BigInt::from(c.alpha()), BigInt::from(255))), Unit::None)),
|
Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)),
|
||||||
Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)),
|
Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)),
|
||||||
_ => todo!("non-color given to builtin function `opacity()`")
|
_ => todo!("non-color given to builtin function `opacity()`")
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
decl!(f "alpha", |args, _| {
|
decl!(f "alpha", |args, _| {
|
||||||
match arg!(args, 0, "color") {
|
match arg!(args, 0, "color") {
|
||||||
Value::Color(c) => Some(Value::Dimension(Number::new(BigRational::new(BigInt::from(c.alpha()), BigInt::from(255))), Unit::None)),
|
Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)),
|
||||||
_ => todo!("non-color given to builtin function `alpha()`")
|
_ => todo!("non-color given to builtin function `alpha()`")
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
pub(crate) use name::ColorName;
|
pub(crate) use name::ColorName;
|
||||||
|
use crate::value::{Number, Value};
|
||||||
|
|
||||||
mod name;
|
mod name;
|
||||||
|
|
||||||
@ -9,17 +10,17 @@ pub(crate) struct Color {
|
|||||||
red: u16,
|
red: u16,
|
||||||
green: u16,
|
green: u16,
|
||||||
blue: u16,
|
blue: u16,
|
||||||
alpha: u16,
|
alpha: Number,
|
||||||
repr: String,
|
repr: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub const fn new(red: u16, green: u16, blue: u16, alpha: u16, repr: String) -> Self {
|
pub fn new(red: u16, green: u16, blue: u16, alpha: u16, repr: String) -> Self {
|
||||||
Color {
|
Color {
|
||||||
red,
|
red,
|
||||||
green,
|
green,
|
||||||
blue,
|
blue,
|
||||||
alpha,
|
alpha: alpha.into(),
|
||||||
repr,
|
repr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,15 +37,15 @@ impl Color {
|
|||||||
self.green
|
self.green
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn alpha(&self) -> u16 {
|
pub fn alpha(&self) -> Number {
|
||||||
self.alpha
|
self.alpha.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_values(red: u16, green: u16, blue: u16, alpha: u16) -> Self {
|
pub fn from_values(red: u16, green: u16, blue: u16, alpha: Number) -> Self {
|
||||||
let repr = if alpha >= 1 {
|
let repr = if alpha >= Number::from(1) {
|
||||||
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
||||||
} else {
|
} else {
|
||||||
format!("#{:0>2x}{:0>2x}{:0>2x}{:0>2x}", red, green, blue, alpha)
|
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
||||||
};
|
};
|
||||||
Color {
|
Color {
|
||||||
red,
|
red,
|
||||||
|
16
src/lexer.rs
16
src/lexer.rs
@ -38,10 +38,24 @@ impl<'a> Iterator for Lexer<'a> {
|
|||||||
'a'..='z' | 'A'..='Z' | '-' | '_' => self.lex_ident(),
|
'a'..='z' | 'A'..='Z' | '-' | '_' => self.lex_ident(),
|
||||||
'@' => self.lex_at_rule(),
|
'@' => self.lex_at_rule(),
|
||||||
'0'..='9' => self.lex_num(),
|
'0'..='9' => self.lex_num(),
|
||||||
|
'.' => {
|
||||||
|
self.buf.next();
|
||||||
|
self.pos.next_char();
|
||||||
|
match self.buf.peek().unwrap() {
|
||||||
|
'0'..='9' => match self.lex_num() {
|
||||||
|
TokenKind::Number(n) => {
|
||||||
|
let mut s = String::from("0.");
|
||||||
|
s.push_str(&n);
|
||||||
|
TokenKind::Number(s)
|
||||||
|
}
|
||||||
|
_ => unsafe { std::hint::unreachable_unchecked() }
|
||||||
|
}
|
||||||
|
_ => TokenKind::Symbol(Symbol::Period)
|
||||||
|
}
|
||||||
|
}
|
||||||
'$' => self.lex_variable(),
|
'$' => self.lex_variable(),
|
||||||
':' => symbol!(self, Colon),
|
':' => symbol!(self, Colon),
|
||||||
',' => symbol!(self, Comma),
|
',' => symbol!(self, Comma),
|
||||||
'.' => symbol!(self, Period),
|
|
||||||
';' => symbol!(self, SemiColon),
|
';' => symbol!(self, SemiColon),
|
||||||
'(' => symbol!(self, OpenParen),
|
'(' => symbol!(self, OpenParen),
|
||||||
')' => symbol!(self, CloseParen),
|
')' => symbol!(self, CloseParen),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::fmt::{self, Display, Write};
|
use std::fmt::{self, Display, Write};
|
||||||
use std::ops::{Add, Sub};
|
use std::ops::{Add, Sub, Mul, Div};
|
||||||
|
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_rational::BigRational;
|
use num_rational::BigRational;
|
||||||
@ -30,6 +30,14 @@ impl From<BigInt> for Number {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<u16> for Number {
|
||||||
|
fn from(b: u16) -> Self {
|
||||||
|
Number {
|
||||||
|
val: BigRational::from_integer(BigInt::from(b)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for Number {
|
impl Display for Number {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", self.val.to_integer())?;
|
write!(f, "{}", self.val.to_integer())?;
|
||||||
@ -72,3 +80,23 @@ impl Sub for Number {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mul for Number {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, other: Self) -> Self {
|
||||||
|
Number {
|
||||||
|
val: self.val * other.val,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div for Number {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn div(self, other: Self) -> Self {
|
||||||
|
Number {
|
||||||
|
val: self.val / other.val,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::iter::{Iterator, Peekable};
|
use std::iter::{Iterator, Peekable};
|
||||||
|
|
||||||
|
use num_rational::BigRational;
|
||||||
|
use num_bigint::BigInt;
|
||||||
|
use num_traits::pow;
|
||||||
|
|
||||||
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;
|
||||||
@ -163,8 +167,29 @@ impl Value {
|
|||||||
} else {
|
} else {
|
||||||
Unit::None
|
Unit::None
|
||||||
};
|
};
|
||||||
|
let n = match val.parse::<BigRational>() {
|
||||||
|
// the number is an integer!
|
||||||
|
Ok(v) => v,
|
||||||
|
// the number is floating point
|
||||||
|
Err(_) => {
|
||||||
|
let mut num = String::new();
|
||||||
|
let mut chars = val.chars().into_iter();
|
||||||
|
let mut num_dec = 0;
|
||||||
|
while let Some(c) = chars.next() {
|
||||||
|
if c == '.' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
num.push(c);
|
||||||
|
}
|
||||||
|
for c in chars {
|
||||||
|
num_dec += 1;
|
||||||
|
num.push(c);
|
||||||
|
}
|
||||||
|
BigRational::new(num.parse().unwrap(), pow(BigInt::from(10), num_dec))
|
||||||
|
}
|
||||||
|
};
|
||||||
Some(Value::Dimension(
|
Some(Value::Dimension(
|
||||||
Number::new(val.parse().expect("error parsing integer")),
|
Number::new(n),
|
||||||
unit,
|
unit,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -64,3 +64,18 @@ test!(
|
|||||||
opacity_function_number_unit,
|
opacity_function_number_unit,
|
||||||
"a {\n color: opacity(1px);\n}\n"
|
"a {\n color: opacity(1px);\n}\n"
|
||||||
);
|
);
|
||||||
|
test!(
|
||||||
|
rgba_opacity_over_1,
|
||||||
|
"a {\n color: rgba(1, 2, 3, 3);\n}\n",
|
||||||
|
"a {\n color: #010203;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
rgba_opacity_decimal,
|
||||||
|
"a {\n color: rgba(1, 2, 3, .6);\n}\n",
|
||||||
|
"a {\n color: rgba(1, 2, 3, 0.6);\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
rgba_opacity_percent,
|
||||||
|
"a {\n color: rgba(1, 2, 3, 50%);\n}\n",
|
||||||
|
"a {\n color: rgba(1, 2, 3, 0.5);\n}\n"
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user