alias sass:color builtin functions

This commit is contained in:
Connor Skees 2020-07-26 00:58:09 -04:00
parent 67fffe7498
commit 3fae0a9621
7 changed files with 66 additions and 29 deletions

View File

@ -205,15 +205,15 @@ fn inner_hsl(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) ->
}
}
fn hsl(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn hsl(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
inner_hsl("hsl", args, parser)
}
fn hsla(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn hsla(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
inner_hsl("hsla", args, parser)
}
fn hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.hue(), Unit::Deg, true)),
@ -225,7 +225,7 @@ fn hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn saturation(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn saturation(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.saturation(), Unit::Percent, true)),
@ -237,7 +237,7 @@ fn saturation(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
}
}
fn lightness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn lightness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.lightness(), Unit::Percent, true)),
@ -249,7 +249,7 @@ fn lightness(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn adjust_hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn adjust_hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,
@ -405,7 +405,7 @@ fn desaturate(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
Ok(Value::Color(Box::new(color.desaturate(amount))))
}
fn grayscale(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn grayscale(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,
@ -426,7 +426,7 @@ fn grayscale(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
Ok(Value::Color(Box::new(color.desaturate(Number::one()))))
}
fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,
@ -441,7 +441,7 @@ fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
Ok(Value::Color(Box::new(color.complement())))
}
fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let weight = match args.default_arg(
1,

View File

@ -1,9 +1,9 @@
use super::{Builtin, GlobalFunctionMap};
mod hsl;
mod opacity;
mod other;
mod rgb;
pub mod hsl;
pub mod opacity;
pub mod other;
pub mod rgb;
pub(crate) fn declare(f: &mut GlobalFunctionMap) {
hsl::declare(f);

View File

@ -5,7 +5,7 @@ use crate::{
value::Value,
};
fn alpha(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn alpha(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None, true)),
@ -17,7 +17,7 @@ fn alpha(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn opacity(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn opacity(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None, true)),
@ -33,6 +33,7 @@ fn opacity(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
// todo: unify `opacify` and `fade_in`
fn opacify(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let color = match args.get_err(0, "color")? {
@ -83,6 +84,7 @@ fn fade_in(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
Ok(Value::Color(Box::new(color.fade_in(amount))))
}
// todo: unify with `fade_out`
fn transparentize(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let color = match args.get_err(0, "color")? {

View File

@ -46,7 +46,7 @@ macro_rules! opt_hsl {
};
}
fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
if args.positional_arg(1).is_some() {
return Err((
"Only one positional argument is allowed. All other arguments must be passed by name.",
@ -113,7 +113,7 @@ fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
}))
}
fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,
v => {
@ -175,8 +175,8 @@ fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
#[allow(clippy::cognitive_complexity)]
// todo: refactor into rgb and hsl?
fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
fn scale(val: Number, by: Number, max: Number) -> Number {
pub(crate) fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn scale(val: Number, by: Number, max: Number) -> Number {
if by.is_zero() {
return val;
}
@ -288,7 +288,7 @@ fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
}))
}
fn ie_hex_str(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn ie_hex_str(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,

View File

@ -336,15 +336,15 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) ->
}
}
fn rgb(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn rgb(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
inner_rgb("rgb", args, parser)
}
fn rgba(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn rgba(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
inner_rgb("rgba", args, parser)
}
fn red(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn red(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.red(), Unit::None, true)),
@ -356,7 +356,7 @@ fn red(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn green(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn green(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.green(), Unit::None, true)),
@ -368,7 +368,7 @@ fn green(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn blue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn blue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(c.blue(), Unit::None, true)),
@ -380,7 +380,7 @@ fn blue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
}
}
fn mix(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
pub(crate) fn mix(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(3)?;
let color1 = match args.get_err(0, "color1")? {
Value::Color(c) => c,

View File

@ -1,4 +1,6 @@
mod functions;
pub(crate) mod modules;
pub(crate) use functions::{list, map, math, meta, selector, string, Builtin, GLOBAL_FUNCTIONS};
pub(crate) use functions::{
color, list, map, math, meta, selector, string, Builtin, GLOBAL_FUNCTIONS,
};

View File

@ -1,5 +1,38 @@
use crate::{
args::CallArgs, builtin::modules::Module, error::SassResult, parse::Parser, value::Value,
args::CallArgs,
builtin::{
color::{
hsl::{complement, grayscale, hue, invert, lightness, saturation},
opacity::alpha,
other::{adjust_color, change_color, ie_hex_str, scale_color},
rgb::{blue, green, mix, red},
},
modules::Module,
},
error::SassResult,
parse::Parser,
value::Value,
};
pub(crate) fn declare(_f: &mut Module) {}
fn adjust_hue(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
todo!()
}
pub(crate) fn declare(f: &mut Module) {
f.insert_builtin("adjust", adjust_color);
f.insert_builtin("alpha", alpha);
f.insert_builtin("blue", blue);
f.insert_builtin("change", change_color);
f.insert_builtin("complement", complement);
f.insert_builtin("grayscale", grayscale);
f.insert_builtin("green", green);
f.insert_builtin("hue", hue);
f.insert_builtin("ie-hex-str", ie_hex_str);
f.insert_builtin("invert", invert);
f.insert_builtin("lightness", lightness);
f.insert_builtin("mix", mix);
f.insert_builtin("red", red);
f.insert_builtin("saturation", saturation);
f.insert_builtin("scale", scale_color);
}