Home Reference Source
import {ColorUtils} from 'color-contrast-calc/lib/color-utils.js'
public class | source

ColorUtils

Collection of functions that provide basic operations on colors represented as RGB/HSL value (given in the form of array of numbers) or hex code (given in the form of string)

Static Method Summary

Static Public Methods
public static

decimalRound(number: number, precision: number): number

Decimal rounding with a given precision

public static

Converts a hex color code string to an HSL representation

public static

Converts a hex color code string to a decimal representation

public static

Converts HSL value to hex code

public static

Converts HSL value to RGB value

public static

isSameHexColor(hexCode1: string, hexCode2: string): boolean

Checks if given two hex color codes represent a same color.

public static

Checks if given two RGB values represent a same color.

public static

Checks if a given object is a string

public static

Checks if a given string is consists of uppercase letters

public static

Checks if a given string is a valid representation of RGB color.

public static

Checks if a given array is a valid representation of HSL color.

public static

Checks if a given array is a valid representation of RGB color.

public static

normalizeHexCode(hexString: string, prefix: boolean): string

Converts a hex color code to a 6-digit hexadecimal string

public static

Converts a decimal representation of color to a hex code string

public static

Converts RGB value to HSL value

Static Public Methods

public static decimalRound(number: number, precision: number): number source

Decimal rounding with a given precision

Params:

NameTypeAttributeDescription
number number

Number to be rounded off

precision number

Number of digits after the decimal point

Return:

number

returns the rounded number

public static hexCodeToHsl(hexCode: string): Array<number, number, number> source

Converts a hex color code string to an HSL representation

Params:

NameTypeAttributeDescription
hexCode string

Hex color code such as "#ffff00"

Return:

Array<number, number, number>

HSL value represented as an array of numbers

public static hexCodeToRgb(hexCode: string): Array<number, number, number> source

Converts a hex color code string to a decimal representation

Params:

NameTypeAttributeDescription
hexCode string

Hex color code such as "#ffff00"

Return:

Array<number, number, number>

RGB value represented as an array of numbers

public static hslToHexCode(hsl: Array<number, number, number>): string source

Converts HSL value to hex code

Params:

NameTypeAttributeDescription
hsl Array<number, number, number>

An array of numbers that represents HSL value

Return:

string

Hex code

public static hslToRgb(hsl: Array<number, number, number>): Array<number, number, number> source

Converts HSL value to RGB value

Params:

NameTypeAttributeDescription
hsl Array<number, number, number>

An array of numbers that represents HSL value

Return:

Array<number, number, number>

An array of numbers that represents RGB value

public static isSameHexColor(hexCode1: string, hexCode2: string): boolean source

Checks if given two hex color codes represent a same color.

Params:

NameTypeAttributeDescription
hexCode1 string

Color given as a hex code, such as "#ffff00", "#FFFF00" or "#ff0"

hexCode2 string

Color given as a hex code, such as "#ffff00", "#FFFF00" or "#ff0"

Return:

boolean

True if given two colors are same

public static isSameRgbColor(rgb1: Array<number, number, number>, rgb2: Array<number, number, number>): boolean source

Checks if given two RGB values represent a same color.

Params:

NameTypeAttributeDescription
rgb1 Array<number, number, number>

Color given as an array of numbers, such as [255, 255, 0]

rgb2 Array<number, number, number>

Color given as an array of numbers, such as [255, 255, 0]

Return:

boolean

True if given two colors are same

public static isString(str: object): boolean source

Checks if a given object is a string

Params:

NameTypeAttributeDescription
str object

Object to be checked

Return:

boolean

returns true if the argument is a string

public static isUpperCase(str: string): boolean source

Checks if a given string is consists of uppercase letters

Params:

NameTypeAttributeDescription
str string

string to be checked

Return:

boolean

returns true if letters in the argument string are all uppercase

public static isValidHexCode(code: string): boolean source

Checks if a given string is a valid representation of RGB color.

Params:

NameTypeAttributeDescription
code string

RGB value in hex code

Return:

boolean

returns true if then argument is a valid RGB color

public static isValidHsl(hsl: Array<number, number, number>): boolean source

Checks if a given array is a valid representation of HSL color.

Params:

NameTypeAttributeDescription
hsl Array<number, number, number>

HSL value represented as an array of numbers

Return:

boolean

true if the argument is a valid HSL color

public static isValidRgb(rgb: Array<number, number, number>): boolean source

Checks if a given array is a valid representation of RGB color.

Params:

NameTypeAttributeDescription
rgb Array<number, number, number>

RGB value represented as an array of numbers

Return:

boolean

true if the argument is a valid RGB color

public static normalizeHexCode(hexString: string, prefix: boolean): string source

Converts a hex color code to a 6-digit hexadecimal string

Params:

NameTypeAttributeDescription
hexString string

String that represent a hex code

prefix boolean
  • optional
  • default: true

Append '#' to the head of return value if a truthy value is given

Return:

string

6-digit hexadecimal string with/without leading '#'

public static rgbToHexCode(rgb: Array<number, number, number>): string source

Converts a decimal representation of color to a hex code string

Params:

NameTypeAttributeDescription
rgb Array<number, number, number>

RGB value represented as an array of numbers

Return:

string

RGB value in hex code

public static rgbToHsl(rgb: Array<number, number, number>): Array<number, number, number> source

Converts RGB value to HSL value

Params:

NameTypeAttributeDescription
rgb Array<number, number, number>

An array of numbers that represents RGB value

Return:

Array<number, number, number>

An array of numbers that represents HSL value