Skip to main content
@spa-tools/utilities

ColorObject

The ColorObject class represents a single color and exposes conversion methods.

Usage

import { ColorObject } from '@spa-tools/utilities';

const color = new ColorObject('#ff0000');

console.log(color.toRgba());
// --> 'rgb(255, 0, 0, 1)'
console.log(color.toHsla());
// --> 'hsla(0, 100%, 50%, 1)'

color.blue = 255;
color.alpha = 0.5;

console.log(color.toHex());
// --> '#ff00ff'
console.log(color.toRgba());
// --> 'rgba(255, 0, 255, 0.5)'

Constructor

NameTypeRequired?DefaultDescription
rednumberyes0Red color value
greennumberyes0Green color value
bluenumberyes0Blue color value
alphanumberno1Alpha color value

Properties

NameTypeReadonly?Description
rednumbernoRed color value
greennumbernoGreen color value
bluenumbernoBlue color value
alphanumbernoAlpha color value

Methods

NameReturn typeDescription
toHexstringReturns the color in hexadecimal format
toRgbastringReturns the color in rgba format
toHslastringReturns the color in hsla format