Colors
Color holds a named color, a hex or RGB value, or a mix of two colors. A color written as a string does not compile.
Building a color
Color has a constant for each CSS named color, generated from MDN's data, and constructors for the other forms:
| Item | Signature | CSS |
|---|---|---|
Color::CRIMSON and the other named colors | const CRIMSON: Color | crimson |
Color::CURRENT_COLOR | const CURRENT_COLOR: Color | currentColor, the element's color |
Color::TRANSPARENT | const TRANSPARENT: Color | transparent |
Color::hex | fn hex(rgb: u32) -> Color | #rrggbb, and panics above 0xffffff |
Color::hex_alpha | fn hex_alpha(rgba: u32) -> Color | #rrggbbaa |
Color::rgb | fn rgb(red: u8, green: u8, blue: u8) -> Color | rgb(r g b) |
Color::hsl | fn hsl(hue: Angle, saturation: Percentage, lightness: Percentage) -> Color | hsl(h s l) |
Color::oklch | fn oklch(lightness: Percentage, chroma: f64, hue: Angle) -> Color | oklch(l c h) |
Color::oklab | fn oklab(lightness: Percentage, a: f64, b: f64) -> Color | oklab(l a b) |
Color::opacity | fn opacity(&self, opacity: u8) -> Color | color-mix(in srgb, <color> <opacity>%, transparent) |
Color::mix | fn mix(&self, other: &Color, weight: u8) -> Color | color-mix(in srgb, <color> <weight>%, <other>) |
Color implements Display, which writes the CSS text:
use hypersig::style::value::{Color, deg, pct};
assert_eq!(Color::CRIMSON.to_string(), "crimson");
assert_eq!(Color::hex(0x00b8_3f45).to_string(), "#b83f45");
assert_eq!(Color::rgb(228, 228, 231).to_string(), "rgb(228 228 231)");
assert_eq!(Color::hsl(deg(210), pct(50), pct(40)).to_string(), "hsl(210deg 50% 40%)");
assert_eq!(Color::oklch(pct(70), 0.1, deg(210)).to_string(), "oklch(70% 0.1 210deg)");
assert_eq!(Color::oklab(pct(70), -0.05, 0.1).to_string(), "oklab(70% -0.05 0.1)");
assert_eq!(Color::CURRENT_COLOR.to_string(), "currentColor");
assert_eq!(Color::TRANSPARENT.to_string(), "transparent");
assert_eq!(
Color::CRIMSON.opacity(20).to_string(),
"color-mix(in srgb, crimson 20%, transparent)"
);
assert_eq!(
Color::CRIMSON.mix(&Color::WHITE, 70).to_string(),
"color-mix(in srgb, crimson 70%, white)"
);opacity and mix take &self and return a new color, so one color can be the base of several. They write CSS's color-mix() in sRGB, so the browser computes the result.
Properties that take a color
color, background_color, and the other properties whose value is only a color take impl Into<Color>. A property with keywords besides colors takes its own enum, which converts from Color: accent_color takes an AccentColor, which also has Auto, and border_color takes a BorderColor. A color written as a string has no conversion, so it is E0277:
use hypersig::style::Style;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::Color: From<&str>`
// is not satisfied
let link = Style::new().color("crimson");A color shared across the page, or one that changes in dark mode, is a token. A Token<Color>'s var() returns a Color, which every one of these methods takes.
What is not checked
Color::hex panics on a value above 0xffffff, so an eight-digit #rrggbbaa literal stops where it is built, and Color::hex_alpha takes it instead. A literal whose red byte is 00, such as 0x00ff_0080, is the number 0xff0080, so hex reads it as #ff0080 and the alpha is lost. opacity and mix read a percentage above 100 as 100:
use hypersig::style::value::Color;
// `#rrggbbaa` goes through `hex_alpha`, and `Color::hex(0x2563_eb80)` panics.
assert_eq!(Color::hex_alpha(0x2563_eb80).to_string(), "#2563eb80");
// A red byte of `00` makes the literal six digits, so `hex` takes it.
assert_eq!(Color::hex(0x00ff_0080).to_string(), "#ff0080");
// A percentage above 100 counts as 100.
assert_eq!(
Color::BLACK.opacity(150).to_string(),
"color-mix(in srgb, black 100%, transparent)"
);For a translucent color, use hex_alpha or opacity. Color::rgb takes u8, so a literal channel above 255 is a compile error from the overflowing_literals lint. Nothing checks the contrast between a text color and its background.
hsl, oklch, and oklab take their channels as typed values, so a hue is an Angle such as deg(210) and a lightness a Percentage. A channel is not checked against its range, and the browser clamps a value outside it. A color function with no constructor, such as lab(), color(), or light-dark(), goes through raw, which Color converts from: color(raw("color(display-p3 1 0.5 0)")). Unchecked values covers raw.
Example: status badges
Each badge is built from one hue: the text in the hue, a fill of the hue at 12%, and a border of 80% hue mixed with black. A new status needs only a new hue:
use hypersig::html::{El, span};
use hypersig::style::Style;
use hypersig::style::value::{BorderStyle, Color, FontWeight, px, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Orders {}
/// A badge tinted by `hue`: a pale fill, a darker border, and text in `hue`.
fn badge(hue: &Color, text: &str) -> El<Orders> {
span()
.css(
Style::new()
.padding_inline(rem(0.5))
.border_width(px(1))
.border_style(BorderStyle::Solid)
.border_color(hue.mix(&Color::BLACK, 80))
.border_radius(rem(1))
.background_color(hue.opacity(12))
.color(hue.clone())
.font_weight(FontWeight::Bold),
)
.text(text)
}
let shipped = Color::hex(0x0016_a34a);
let late = Color::hex(0x00dc_2626);
let html = Page::new("Orders", Orders {})
.child(badge(&shipped, "Shipped"))
.child(badge(&late, "Late"))
.render();Next, Angles and times covers rotations and durations.