Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/colors/mod.ts>Color

Color conversion and stuff in TypeScript
Go to Latest
class Color
import { Color } from "https://deno.land/x/colors@v1.2.1/mod.ts";

General class for RGBA colors

Constructors

new
Color(hex: string)

Construct a color from hex code

new
Color(hex: number)

Construct a color from hex number

new
Color(
r: number,
g: number,
b: number,
a?: number,
)

Construct a color from rgba values

new
Color(
rOrHex: number | string,
g?: number,
b?: number,
a?,
)

Properties

a: number

Alpha (opacity) of color

readonly
average

Get the average of all colors Can also be used instead of grayscale using

const color = new Color(r, g, b, a);
const avg = color.average;
const grayscaleColor = new Color(avg, avg, avg, a);
b: number

Blue value of color

readonly
chroma

Calculate chroma

readonly
cmyk:
[
number,
number,
number,
number,
]
g: number

Green value of color

readonly
grayscale: Color

Convert to grayscale using luminance

readonly
hcg: [number, number, number]
readonly
hex
readonly
hsl: [number, number, number]

Hue, Saturation, Lightness

readonly
hsv: [number, number, number]

Hue, Saturation, Value

readonly
hue

Calculate hue using chroma

readonly
invert
readonly
lab: [number, number, number]

CIE Lab color space

readonly
lightness

Get lightness of image. Can also be used instead of grayscale using

const color = new Color(r, g, b, a);
const l = color.lightness * 255;
const grayscaleColor = new Color(l, l, l, a);
readonly
linearRgb

Get linear rgb values

readonly
luminance: number

Calculate luminance

readonly
max: number

Get maximum of r, g, b

readonly
min: number

Get minimum of r, g, b

readonly
perceivedLightness: number

Get perceived lightness

r: number

Red value of color

readonly
saturation

Get saturation

readonly
xyz: [number, number, number]

CIE 1931 XYZ

Methods

contrast(that: Color): number

Get contrast ratio

mix(that: Color, percentage?): Color

Mix with a different color. Copyright (c) 2006-2009 Hampton Catlin, Natalie Weizenbaum, and Chris Eppstein http://sass-lang.com

shade(weight?): Color

Get a shade of the color

tint(weight?): Color

Get a tint of the color

toJSON(): ColorData

Get a detailed conversion of the color.

toString(): string

Static Methods

fromCmyk(
c: number,
m: number,
y: number,
k: number,
): Color
fromHex(hex: string): Color
fromHsl(
h: number,
s: number,
l: number,
): Color
fromHsv(
h: number,
s: number,
v: number,
): Color
fromLab(
l: number,
a: number,
b: number,
): Color
fromRgba(
r: number,
g: number,
b: number,
): Color

Redundant static method for conversion from sRGB color space

fromXyz(
x: number,
y: number,
z: number,
): Color

Convert from CIE XYZ color space

toHex(n: number): string