Skip to content

Colour class fixes and improvements: - #117

Open
liamegan wants to merge 1 commit into
fix/matricesfrom
fix/color
Open

liamegan wants to merge 1 commit into
fix/matricesfrom
fix/color

Conversation

@liamegan

Copy link
Copy Markdown
Member
  • Color.fromHSV wraps any hue, so -120 gives the same colour as 240 instead of throwing.
  • Color.fromHex reads alpha from 4- and 8-digit hex, so '#ff000080' is red at about half opacity. It now throws an Error for anything that isn't hex; before, those strings gave black.
  • CSS strings are now supported through a new Color.fromCSS. It reads:
    • hex
    • rgb() and rgba()
    • hsl() and hsla(), with an optional deg on the hue
  • toHex and toCSS clamp each channel to 0–1 before formatting.

- Color.fromHSV wraps any hue, so -120 gives the same colour as 240 instead of throwing.
- Color.fromHex reads alpha from 4- and 8-digit hex, so '#ff000080' is red at about half opacity. It now throws an Error for anything that isn't hex; before, those strings gave black.
- CSS strings are now supported through a new Color.fromCSS. It reads:
  - hex
  - rgb() and rgba()
  - hsl() and hsla(), with an optional deg on the hue

  The comma syntax (rgba(255, 136, 0, 0.5)) and the space syntax (rgb(255 136 0 / 50%)) both work, and channels can be percentages. Color.interpolate uses it for strings, so any option that takes a colour now accepts CSS strings. Named colours such as 'red' throw.
- toHex and toCSS clamp each channel to 0–1 before formatting, and toCSS clamps alpha too, so they always give a valid string. The colour itself isn't changed.
@liamegan
liamegan added this pull request to stack #115 November 26, 2026 17:32
Comment thread src/Color.ts
* @returns A new colour.
*/
static fromCSS(css: string): Color {
const match = /^\s*(rgba?|hsla?)\(\s*([^)]*)\)\s*$/i.exec(css);
Comment thread src/Color.ts
if (!match) return Color.fromHex(css);

const fn = match[1].toLowerCase();
const parts = match[2].split(/\s*[,/]\s*|\s+/).filter(Boolean);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants