Skip to content

Commit

Permalink
fix: use hex -> rgb(a) without hsla where possible (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Dec 10, 2023
1 parent 0f39942 commit c7c095f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
24 changes: 12 additions & 12 deletions whiskers/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base64::Engine;
use css_colors::{Color, Ratio, HSLA};
use css_colors::{Color, Ratio, HSLA, RGBA};
use handlebars::{
handlebars_helper, Context, Handlebars, Helper, HelperResult, Output, RenderContext,
RenderError,
Expand All @@ -8,7 +8,7 @@ use handlebars::{
use ::titlecase::titlecase as titlecase_ext;
use serde_json::Value;

use crate::parse::HSLAExt;
use crate::parse::ColorExt;

impl From<crate::parse::Error> for RenderError {
fn from(value: crate::parse::Error) -> Self {
Expand All @@ -33,10 +33,10 @@ handlebars_helper!(opacity: |color: String, amount: f32| {
HSLA::from_hex(&color)?.fade(Ratio::from_f32(amount)).to_hex()
});
handlebars_helper!(rgb: |color: String| {
HSLA::from_hex(&color)?.to_rgb().to_string()
RGBA::from_hex(&color)?.to_rgb().to_string()
});
handlebars_helper!(rgba: |color: String| {
HSLA::from_hex(&color)?.to_rgba().to_string()
RGBA::from_hex(&color)?.to_string()
});
handlebars_helper!(hsl: |color: String| {
HSLA::from_hex(&color)?.to_hsl().to_string()
Expand All @@ -45,28 +45,28 @@ handlebars_helper!(hsla: |color: String| {
HSLA::from_hex(&color)?.to_string()
});
handlebars_helper!(red_i: |color: String| {
HSLA::from_hex(&color)?.to_rgba().r.as_u8()
RGBA::from_hex(&color)?.r.as_u8()
});
handlebars_helper!(green_i: |color: String| {
HSLA::from_hex(&color)?.to_rgba().g.as_u8()
RGBA::from_hex(&color)?.g.as_u8()
});
handlebars_helper!(blue_i: |color: String| {
HSLA::from_hex(&color)?.to_rgba().b.as_u8()
RGBA::from_hex(&color)?.b.as_u8()
});
handlebars_helper!(alpha_i: |color: String| {
HSLA::from_hex(&color)?.to_rgba().a.as_u8()
RGBA::from_hex(&color)?.a.as_u8()
});
handlebars_helper!(red_f: |color: String| {
HSLA::from_hex(&color)?.to_rgba().r.as_f32()
RGBA::from_hex(&color)?.r.as_f32()
});
handlebars_helper!(green_f: |color: String| {
HSLA::from_hex(&color)?.to_rgba().g.as_f32()
RGBA::from_hex(&color)?.g.as_f32()
});
handlebars_helper!(blue_f: |color: String| {
HSLA::from_hex(&color)?.to_rgba().b.as_f32()
RGBA::from_hex(&color)?.b.as_f32()
});
handlebars_helper!(alpha_f: |color: String| {
HSLA::from_hex(&color)?.to_rgba().a.as_f32()
RGBA::from_hex(&color)?.a.as_f32()
});

pub fn darklight(
Expand Down
20 changes: 15 additions & 5 deletions whiskers/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub enum Error {
ParseInt(ParseIntError),
}

pub trait HSLAExt {
fn from_hex(hex: &str) -> Result<HSLA, Error>;
pub trait ColorExt<T: Color> {
fn from_hex(hex: &str) -> Result<T, Error>;
fn to_hex(&self) -> String;
}

Expand All @@ -24,7 +24,7 @@ fn hex_to_u8s(hex: &str) -> Result<Vec<u8>, ParseIntError> {
.collect()
}

impl HSLAExt for HSLA {
impl ColorExt<Self> for RGBA {
fn from_hex(hex: &str) -> Result<Self, Error> {
if hex.len() != 6 && hex.len() != 8 {
return Err(Error::InvalidLength(hex.len()));
Expand All @@ -36,15 +36,15 @@ impl HSLAExt for HSLA {
.expect("guaranteed to have at least 3 elements");
let alpha = components.get(3).copied().unwrap_or(255);

Ok(rgba(red, green, blue, Ratio::from_u8(alpha).as_f32()).to_hsla())
Ok(rgba(red, green, blue, Ratio::from_u8(alpha).as_f32()))
}

fn to_hex(&self) -> String {
if self.a.as_u8() == 255 {
let RGB { r, g, b } = self.to_rgb();
format!("{:02x}{:02x}{:02x}", r.as_u8(), g.as_u8(), b.as_u8())
} else {
let RGBA { r, g, b, a } = self.to_rgba();
let Self { r, g, b, a } = self;
format!(
"{:02x}{:02x}{:02x}{:02x}",
r.as_u8(),
Expand All @@ -56,5 +56,15 @@ impl HSLAExt for HSLA {
}
}

impl ColorExt<Self> for HSLA {
fn from_hex(hex: &str) -> Result<Self, Error> {
Ok(RGBA::from_hex(hex)?.to_hsla())
}

fn to_hex(&self) -> String {
self.to_rgba().to_hex()
}
}

#[cfg(test)]
mod tests {}

0 comments on commit c7c095f

Please sign in to comment.