On This Page
Functions
Color Utilities
The Color utilities provide functions for working with different color formats and performing conversions.
Functions
oklchToRgb
function oklchToRgb(oklchString)Converts an OKLCH color string to an RGB object. Handles potential out-of-gamut colors by clamping the final RGB values. Based on https://bottosson.github.io/posts/oklab/.
Parameters
| Name | Type | Description |
|---|---|---|
| oklchString | string | The color string in the format “oklch(L C H)” or “oklch(L, C, H)”. L: Lightness (0-1), C: Chroma (0-~0.4), H: Hue (0-360). |
Returns
An object with r, g, b properties (0-255), or null if the input string is invalid.
Example
import { oklchToRgb } from '@semantic-ui/utils';
// Convert a specific OKLCH color (approx. red)const rgbRed = oklchToRgb('oklch(0.628 0.257 29.23)');console.log(rgbRed); // { r: 255, g: 0, b: 0 }
// Convert grayconst rgbGray = oklchToRgb('oklch(0.5, 0, 0)');console.log(rgbGray); // { r: 119, g: 119, b: 119 }
// Handle invalid inputconst invalid = oklchToRgb('not a color');console.log(invalid); // nulloklchToHex
function oklchToHex(oklchString)Converts an OKLCH color string to a hex color string (e.g., “#RRGGBB”). This function internally uses oklchToRgb.
Parameters
| Name | Type | Description |
|---|---|---|
| oklchString | string | The color string in the format “oklch(L C H)” or “oklch(L, C, H)”. |
Returns
The hex color string (e.g., “#FF0000”) or null if the input string is invalid.
Example
import { oklchToHex } from '@semantic-ui/utils';
// Convert a specific OKLCH color (approx. red)const hexRed = oklchToHex('oklch(0.628 0.257 29.23)');console.log(hexRed); // "#ff0000"
// Convert grayconst hexGray = oklchToHex('oklch(0.5, 0, 0)');console.log(hexGray); // "#777777"
// Handle invalid inputconst invalidHex = oklchToHex('not a color');console.log(invalidHex); // null