Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement hex formatting support #172

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions whiskers/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn mix(
.as_f64()
.ok_or_else(|| tera::Error::msg("blend amount must be a number"))?;

let result = Color::mix(&base, &blend, amount);
let result = Color::mix(&base.clone(), &blend, amount, base.hex_format);

Ok(tera::to_value(result)?)
}
Expand All @@ -33,18 +33,21 @@ pub fn modify(
args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let hex_format = color.clone().hex_format;
if let Some(hue) = args.get("hue") {
let hue = tera::from_value(hue.clone())?;
Ok(tera::to_value(color.mod_hue(hue))?)
Ok(tera::to_value(color.mod_hue(hue, hex_format))?)
} else if let Some(saturation) = args.get("saturation") {
let saturation = tera::from_value(saturation.clone())?;
Ok(tera::to_value(color.mod_saturation(saturation))?)
Ok(tera::to_value(
color.mod_saturation(saturation, hex_format),
)?)
} else if let Some(lightness) = args.get("lightness") {
let lightness = tera::from_value(lightness.clone())?;
Ok(tera::to_value(color.mod_lightness(lightness))?)
Ok(tera::to_value(color.mod_lightness(lightness, hex_format))?)
} else if let Some(opacity) = args.get("opacity") {
let opacity = tera::from_value(opacity.clone())?;
Ok(tera::to_value(color.mod_opacity(opacity))?)
Ok(tera::to_value(color.mod_opacity(opacity, hex_format))?)
} else {
Ok(value.clone())
}
Expand All @@ -55,18 +58,21 @@ pub fn add(
args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let hex_format = color.clone().hex_format;
if let Some(hue) = args.get("hue") {
let hue = tera::from_value(hue.clone())?;
Ok(tera::to_value(color.add_hue(hue))?)
Ok(tera::to_value(color.add_hue(hue, hex_format))?)
} else if let Some(saturation) = args.get("saturation") {
let saturation = tera::from_value(saturation.clone())?;
Ok(tera::to_value(color.add_saturation(saturation))?)
Ok(tera::to_value(
color.add_saturation(saturation, hex_format),
)?)
} else if let Some(lightness) = args.get("lightness") {
let lightness = tera::from_value(lightness.clone())?;
Ok(tera::to_value(color.add_lightness(lightness))?)
Ok(tera::to_value(color.add_lightness(lightness, hex_format))?)
} else if let Some(opacity) = args.get("opacity") {
let opacity = tera::from_value(opacity.clone())?;
Ok(tera::to_value(color.add_opacity(opacity))?)
Ok(tera::to_value(color.add_opacity(opacity, hex_format))?)
} else {
Ok(value.clone())
}
Expand All @@ -77,18 +83,21 @@ pub fn sub(
args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let hex_format = color.clone().hex_format;
if let Some(hue) = args.get("hue") {
let hue = tera::from_value(hue.clone())?;
Ok(tera::to_value(color.sub_hue(hue))?)
Ok(tera::to_value(color.sub_hue(hue, hex_format))?)
} else if let Some(saturation) = args.get("saturation") {
let saturation = tera::from_value(saturation.clone())?;
Ok(tera::to_value(color.sub_saturation(saturation))?)
Ok(tera::to_value(
color.sub_saturation(saturation, hex_format),
)?)
} else if let Some(lightness) = args.get("lightness") {
let lightness = tera::from_value(lightness.clone())?;
Ok(tera::to_value(color.sub_lightness(lightness))?)
Ok(tera::to_value(color.sub_lightness(lightness, hex_format))?)
} else if let Some(opacity) = args.get("opacity") {
let opacity = tera::from_value(opacity.clone())?;
Ok(tera::to_value(color.sub_opacity(opacity))?)
Ok(tera::to_value(color.sub_opacity(opacity, hex_format))?)
} else {
Ok(value.clone())
}
Expand Down
35 changes: 20 additions & 15 deletions whiskers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ use whiskers::{

const FRONTMATTER_OPTIONS_SECTION: &str = "whiskers";

#[derive(Default, Debug, serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
struct TemplateOptions {
version: Option<semver::VersionReq>,
matrix: Option<Matrix>,
filename: Option<String>,
hex_prefix: Option<String>,
#[serde(default)]
capitalize_hex: bool,
hex_format: String,
}

impl Default for TemplateOptions {
fn default() -> Self {
Self {
version: None,
matrix: None,
filename: None,
hex_format: "{{r}}{{g}}{{b}}".to_string(),
}
}
}

impl TemplateOptions {
Expand All @@ -41,9 +50,7 @@ impl TemplateOptions {
version: Option<semver::VersionReq>,
matrix: Option<Vec<tera::Value>>,
filename: Option<String>,
hex_prefix: Option<String>,
#[serde(default)]
capitalize_hex: bool,
hex_format: Option<String>,
}

if let Some(opts) = frontmatter.get(FRONTMATTER_OPTIONS_SECTION) {
Expand All @@ -58,8 +65,9 @@ impl TemplateOptions {
version: opts.version,
matrix,
filename: opts.filename,
hex_prefix: opts.hex_prefix,
capitalize_hex: opts.capitalize_hex,
hex_format: opts
.hex_format
.unwrap_or_else(|| "{{r}}{{g}}{{b}}".to_string()),
})
} else {
Ok(Self::default())
Expand Down Expand Up @@ -116,12 +124,8 @@ fn main() -> anyhow::Result<()> {
}

// build the palette and add it to the templating context
let palette = models::build_palette(
template_opts.capitalize_hex,
template_opts.hex_prefix.as_deref(),
args.color_overrides.as_ref(),
)
.context("Palette context cannot be built")?;
let palette = models::build_palette(template_opts.hex_format, args.color_overrides.as_ref())
.context("Palette context cannot be built")?;

ctx.insert("flavors", &palette.flavors);
if let Some(flavor) = args.flavor {
Expand Down Expand Up @@ -257,6 +261,7 @@ fn render_single_output(
fn render_multi_output(
matrix: HashMap<String, Vec<String>>,
filename_template: &str,
// hex_template: &str,
ctx: &tera::Context,
palette: &models::Palette,
tera: &tera::Tera,
Expand Down
Loading
Loading