Skip to content

Commit

Permalink
feat(whiskers): add rgba to hex helpers (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anomalocaridid committed Feb 13, 2024
1 parent 17750d8 commit 31ffd9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions whiskers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ The following custom helpers are available:
| green_f \<color\> | `{{ green_f red }}` | `0.55` (truncated to 2 places) | Get the green channel of a color as a float from 0 to 1. |
| blue_f \<color\> | `{{ blue_f red }}` | `0.66` (truncated to 2 places) | Get the blue channel of a color as a float from 0 to 1. |
| alpha_f \<color\> | `{{ alpha_f (opacity red 0.6) }}` | `0.60` (truncated to 2 places) | Get the alpha channel of a color as a float from 0 to 1. |
| red_h \<color\> | `{{ red_h red }}` | `f3` | Get the red channel of a color as a hexadecimal number from 00 to ff. |
| green_h \<color\> | `{{ green_h red }}` | `8b` | Get the green channel of a color as a hexadecimal number from 00 to ff. |
| blue_h \<color\> | `{{ blue_h red }}` | `a8` | Get the blue channel of a color as a hexadecimal number from 00 to ff. |
| alpha_h \<color\> | `{{ alpha_h (opacity red 0.6) }}` | `99` | Get the alpha channel of a color as a hexadecimal number from 00 to ff. |
| darklight \<if-dark\> \<if-light\> | `{{ darklight "Night" "Day" }}` | `Day` on Latte, `Night` on other flavors | Choose a value depending on the current flavor. Latte is light, while Frappé, Macchiato, and Mocha are all dark. |

## Frontmatter
Expand Down
12 changes: 12 additions & 0 deletions whiskers/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ handlebars_helper!(blue_f: |color: String| {
handlebars_helper!(alpha_f: |color: String| {
RGBA::from_hex(&color)?.a.as_f32()
});
handlebars_helper!(red_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.r.as_u8())
});
handlebars_helper!(green_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.g.as_u8())
});
handlebars_helper!(blue_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.b.as_u8())
});
handlebars_helper!(alpha_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.a.as_u8())
});

pub fn darklight(
h: &Helper,
Expand Down
28 changes: 28 additions & 0 deletions whiskers/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,34 @@ pub fn helpers() -> Vec<Helper> {
examples: &[("(opacity red 0.6)", "`0.60` (truncated to 2 places)")],
handler: Box::new(helper::alpha_f),
},
Helper {
name: "red_h",
description: "Get the red channel of a color as hex digits from 00 to FF.",
args: &["color"],
examples: &[("red", "`f3`")],
handler: Box::new(helper::red_h),
},
Helper {
name: "green_h",
description: "Get the green channel of a color as hex digits from 00 to FF.",
args: &["color"],
examples: &[("red", "`8b`")],
handler: Box::new(helper::green_h),
},
Helper {
name: "blue_h",
description: "Get the blue channel of a color as hex digits from 00 to FF.",
args: &["color"],
examples: &[("red", "`a8`")],
handler: Box::new(helper::blue_h),
},
Helper {
name: "alpha_h",
description: "Get the alpha channel of a color as hex digits from 00 to FF.",
args: &["color"],
examples: &[("(opacity red 0.6)", "`99`")],
handler: Box::new(helper::alpha_h),
},
Helper {
name: "darklight",
description: "Choose a value depending on the current flavor. Latte is light, while Frappé, Macchiato, and Mocha are all dark.",
Expand Down

0 comments on commit 31ffd9e

Please sign in to comment.