Implement builtin function ie-hex-str()
This commit is contained in:
parent
19f59efd98
commit
a35fa119e0
@ -2,6 +2,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use super::Builtin;
|
||||
use crate::color::Color;
|
||||
use crate::common::QuoteKind;
|
||||
use crate::units::Unit;
|
||||
use crate::value::{Number, Value};
|
||||
|
||||
@ -172,6 +173,13 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
color
|
||||
}))
|
||||
});
|
||||
decl!(f "ie-hex-str", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c.clone(),
|
||||
_ => todo!("non-color given to builtin function `ie-hex-str()`")
|
||||
};
|
||||
Some(Value::Ident(color.to_ie_hex_str(), QuoteKind::None))
|
||||
});
|
||||
}
|
||||
|
||||
fn scale(val: Number, by: Number, max: Number) -> Number {
|
||||
|
@ -396,6 +396,21 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
/// Other color functions
|
||||
impl Color {
|
||||
pub fn to_ie_hex_str(&self) -> String {
|
||||
format!(
|
||||
"#{:02X}{:02X}{:02X}{:02X}",
|
||||
(self.alpha.clone() * Number::from(255))
|
||||
.round()
|
||||
.to_integer(),
|
||||
self.red.to_integer(),
|
||||
self.green.to_integer(),
|
||||
self.blue.to_integer()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the proper representation from RGBA values
|
||||
fn repr(red: &Number, green: &Number, blue: &Number, alpha: &Number) -> String {
|
||||
macro_rules! into_u8 {
|
||||
|
@ -441,3 +441,18 @@ test!(
|
||||
"a {\n color: scale-color(sienna, $alpha: -70%);\n}\n",
|
||||
"a {\n color: rgba(160, 82, 45, 0.3);\n}\n"
|
||||
);
|
||||
test!(
|
||||
ie_hex_str_hex_3,
|
||||
"a {\n color: ie-hex-str(#abc);\n}\n",
|
||||
"a {\n color: #FFAABBCC;\n}\n"
|
||||
);
|
||||
test!(
|
||||
ie_hex_str_hex_6,
|
||||
"a {\n color: ie-hex-str(#3322BB);\n}\n",
|
||||
"a {\n color: #FF3322BB;\n}\n"
|
||||
);
|
||||
test!(
|
||||
ie_hex_str_rgb,
|
||||
"a {\n color: ie-hex-str(rgba(0, 255, 0, 0.5));\n}\n",
|
||||
"a {\n color: #8000FF00;\n}\n"
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user