API Reference
Complete JavaScript function reference for OmniColor's color utility API. All functions are exposed on the global window.OMNI_COLOR object and documented below.
Overview
OmniColor does not have a hosted API. All functions below are exposed by /assets/js/color-utils.js which loads on every page. You can use them in your own scripts or from the browser console.
Example usage in console:
OMNI_COLOR.hexToRgb('#FF7F50')
// → { r: 255, g: 127, b: 80 }
Format Conversion
hexToRgb(hex)
Convert a HEX color string to RGB. Accepts 3-digit, 4-digit, 6-digit or 8-digit HEX. Invalid hex returns {0,0,0}.
hex string — e.g. '#FF7F50' or 'FF7F50'
hexToRgba(hex, alpha)
Convert HEX to RGBA. Alpha defaults to 1 (opaque).
hex string — HEX color
alpha number (optional) — 0 to 1
rgbToHex(r, g, b)
Convert RGB channels to uppercase HEX. Channels are clamped to 0-255.
r, g, b number — 0-255
rgbaToHex(r, g, b, a)
Convert RGBA to 6 or 8-digit HEX. If alpha = 1, returns 6-digit; else 8-digit.
rgbToHsl(r, g, b)
Convert RGB to HSL. Hue is 0-360, saturation and lightness are 0-100.
hslToRgb(h, s, l)
Convert HSL to RGB. Hue is wrapped modulo 360. Saturation and lightness are clamped to 0-100.
rgbToHsv(r, g, b)
Convert RGB to HSV. Similar to HSL but uses value (brightness) instead of lightness.
hsvToRgb(h, s, v)
Convert HSV to RGB.
rgbToCmyk(r, g, b)
Convert RGB to CMYK percentages (0-100 each). Note: this is approximate — screen and print color spaces differ.
cmykToRgb(c, m, y, k)
Convert CMYK percentages to RGB. Approximate — use with awareness of gamut differences.
Validation
isValidHex(hex)
Check if a value is a valid HEX color string. Accepts 3, 4, 6 or 8-digit HEX with or without #.
normalizeHex(hex)
Normalize a HEX string to 6-digit uppercase with # prefix. Invalid input returns '#000000'.
Contrast & Accessibility
luminance(hex)
Compute WCAG relative luminance of a color (0 = black, 1 = white). Uses the exact WCAG 2.2 formula with sRGB gamma correction.
contrastRatio(hex1, hex2)
Compute WCAG contrast ratio as a string with 2 decimals (e.g. "4.53"). Range is 1.00 to 21.00.
hex1, hex2 — any valid HEX strings
Formatting
toRgbString(hex)
Format a HEX as CSS rgb(r, g, b) string.
toHslString(hex)
Format a HEX as CSS hsl(h, s%, l%) string.
toCmykString(hex)
Format a HEX as CSS cmyk(c%, m%, y%, k%) notation.
Color Manipulation
mix(hex1, hex2, ratio)
Blend two colors. Ratio 0.5 (default) returns midpoint. 0 = hex1, 1 = hex2.
lighten(hex, amount)
Increase lightness by N percentage points (default 10). Returns new HEX.
darken(hex, amount)
Decrease lightness by N percentage points (default 10). Returns new HEX.
Harmony
complementary(hex)
Return the complementary color (hue + 180°).
analogous(hex, angle)
Return 3 analogous colors: [hex - angle, hex, hex + angle]. Default angle 30°.
triadic(hex)
Return 3 triadic colors: [hex, hex+120°, hex+240°].
Clamp Helper
clamp(value, min, max)
Clamp a number between min and max. Internal utility used by all conversion functions.
Usage Examples
Generate a palette in the console
// Triadic palette from brand blue
const base = '#155EEF';
const triad = OMNI_COLOR.triadic(base);
console.log(triad);
// → ['#155EEF', '#EF155E', '#5EEF15']
// Complementary color
const comp = OMNI_COLOR.complementary(base);
console.log(comp);
// → '#EF5515'
// Check if a color pair is WCAG AA accessible
const ratio = OMNI_COLOR.contrastRatio('#15243B', '#FFFFFF');
console.log(ratio); // '14.53'
Build a color scale
// Generate 10 shades of any color
function generateShades(baseHex) {
const rgb = OMNI_COLOR.hexToRgb(baseHex);
const hsl = OMNI_COLOR.rgbToHsl(rgb.r, rgb.g, rgb.b);
const shades = [];
for (let i = 0; i < 10; i++) {
const l = 5 + i * 10;
const newRgb = OMNI_COLOR.hslToRgb(hsl.h, hsl.s, l);
shades.push(OMNI_COLOR.rgbToHex(newRgb.r, newRgb.g, newRgb.b));
}
return shades;
}
generateShades('#155EEF');
// → ['#011B44', '#043B8C', '#0A5BD1', '#1576E7', '#3492F3',
// '#66AEF7', '#98C9FA', '#C8DFFC', '#E6EFFE', '#FAFCFF']
Cross-References
- API Docs — algorithmic documentation with formulas
- Documentation — architecture and configuration
- Developer Portal — code examples and best practices