Collection of useful methods for colors parsing, validation, transformations and calculations. Exposed as a class, so can be used as a stand-alone, or inside Angular apps.
npm install @kolory/color-utilities
Use in a JavaScript or TypeScript project:
// TypeScript, JavaScript module
import {ColorUtilities} from '@kolory/color-utilities'
// Node, Browserify
const ColorUtilities = require('@kolory/color-utilities')
// Create using a `new` keyword or with a factory (preferred).
const colorUtilitiesFromFactory = ColorUtilities.create() // Preferred.
const colorUtilities = new ColorUtilities()
Library is compatible with the Angular 2 dependency injection system. Provide it as a dependency in a Component or NgModule decorator.
@Component({
...
providers: [ColorUtilities, ...]
})
class MyComponent {
consturctor(private colorUtilities: ColorUtilities) {}
}
#convert(color: basicColor, to: ColorTypes | 'hex' | 'rgb' | 'hsl'): basicColor
#convertToHex(color: basicColor): basicColor
#convertToRgb(color: basicColor): basicColor
#convertToHsl(color: basicColor): basicColor
Converts a color into a different format (eg. hex to RGB or HSL to hex). Use either the generic convert
method providing
a color to be converted with the outcome format (from the ColorType
enum or as a string) or one of the specific method .
color
- A valid color that will be converted.
to
- (in the #convert
generic method) a color type (coming from the ColorTypes
enum) into which the color should be converted.
A valid color in another format.
import {ColorTypes} from '@kolory/color-utilities'
colorUtilities.convert('#F03402', ColorTypes.hsl) // => 'hsl(13, 98%, 47%)'
colorUtilities.convert('hsl(39, 100%, 50%)', ColorTypes.rgb) // => 'rgb(255, 166, 0)'
colorUtil.convertToRgb('#FFA500') // => 'rgb(255, 165, 0)'
colorUtil.convertToHsl('rgb(255, 166, 0)') // => 'hsl(39, 100%, 50%)'
colorUtil.convertToHex('hsl(39, 100%, 50%)') // => '#FFA600'
#convertRawValuesTo(values: colorValues, to: ColorTypes): basicColor
Converts a raw color values (a triplet array: [R, G, B]) into a formatted string (eg. hex to RGB or HSL to hex).
values
- A raw values.
to
- a color type (coming from the ColorTypes
enum) into which the color should be converted.
A valid color in another format.
colorUtilities.convertRawValuesTo([255, 165, 0], ColorTypes.hex) // => '#FFA500'
#parseColor(color: basicColor): colorValues
Transforms RGB, HSL or hex color into an array of RGB values. A generic variant of specific parsers dedicated for hex and RGB colors.
color
- A color in RGB, HSL or hex format to be parsed.
An array representation of the provided number. Throws if an invalid color was used (use validators methods id unsure if the value will always be correct).
colorUtilities.parseColor('#FFAA500') // => [255, 165, 0]
colorUtilities.parseColor('#fff') // => [255, 255, 255]
colorUtilities.parseColor('rgb(255, 165, 0)') // => [255, 165, 0]
colorUtilities.parseColor('rgb(255, 255, 255)') // => [255, 255, 255]
colorUtilities.parseColor('hsl(0, 100%, 100%)') // => [255, 255, 255]
#parseHexColor(color: hexColor): colorValues
Transforms a hexadecimal color into an array of RGB values in base-10.
color
- A hexadecimal color (eg. '#FFA500'). Shorthand notation ('#FFF')
and small caps are also supported.
An array representation of the provided number. Throws if an invalid hex was used (use validators methods id unsure if the value will always be correct).
colorUtilities.parseHexColor('#FFAA500') // => [255, 165, 0]
colorUtilities.parseHexColor('#fff') // => [255, 255, 255]
#parseRgbColor(color: RGBColor): colorValues
Transforms an RGB color into an array of RGB values.
color
- An RGB color (eg. 'rgb(255, 265, 0)').
An array representation of the provided number. Throws if an invalid RGB format was used (use validators methods id unsure if the value will always be correct).
colorUtilities.parseHexColor('rgb(255, 165, 0)') // => [255, 165, 0]
colorUtilities.parseHexColor('rgb(255, 255, 255)') // => [255, 255, 255]
#parseHslColor(color: HSLColor): colorValues
Transforms an HSL color into an array of RGB values.
color
- An HSL color (eg. 'hsl(123, 45%, 0%)').
An array representation of the provided number. Throws if an invalid HSL format was used (use validators methods id unsure if the value will always be correct).
colorUtilities.parseHslColor('hsl(123, 100%, 100%)') // => [255, 255, 255]
#isValidColor(potentialColor?: basicColor): boolean
Checks if the provided color is a valid hexadecimal color, an RGB color or an HSL color. Uses specific validators internally, so the edge cases from them applies here.
color
- A color to be validated.
A boolean indicating validity of the color. For clarity sake: true
means the color is valid.
colorUtilities.isValidColor('#FFA500') // => true
colorUtilities.isValidColor('rgb(255, 165, 0)') // => true
colorUtilities.isValidColor('hsl(200, 100%, 50%)') // => true
colorUtilities.isValidColor('#QWERTY') // => false
colorUtilities.isValidColor('#123456789') // => false
colorUtilities.isValidColor(null) // => false
#isValidHexColor(potentialHexColor?: hexColor): boolean
Checks if the provided color is a valid hexadecimal color.
color
- A hexadecimal color (eg. '#FFA500'). Shorthand notation ('#FFF')
and small caps are also supported.
A boolean indicating validity of the color. For clarity sake: true
means the color is valid.
colorUtilities.isValidHexColor('#FFA500') // => true
colorUtilities.isValidHexColor('#fff') // => true
colorUtilities.isValidHexColor('#QWERTY') // => false
colorUtilities.isValidHexColor('#123456789') // => false
colorUtilities.isValidHexColor(null) // => false
#isValidRgbColor(potentialRgbColor?: RGBColor): boolean
Checks if the provided color is a valid RGB color.
color
- An RGB color (eg. 'rgb(255, 165, 0)').
A boolean indicating validity of the color. For clarity sake: true
means the color is valid.
colorUtilities.isValidRgbColor('rgb(0, 0, 0)') // => true
colorUtilities.isValidRgbColor('rgb(255, 165, 0') // => true
colorUtilities.isValidRgbColor('xrgb(255, 255, 255') // => false
colorUtilities.isValidRgbColor('rgb(300, 300, 300') // => false
colorUtilities.isValidRgbColor(null) // => false
#isValidHslColor(potentialHslColor?: HSLColor): boolean
Checks if the provided color is a valid HSL color.
color
- An HSL color (eg. 'hsl(300, 50%, 50%)').
A boolean indicating validity of the color. For clarity sake: true
means the color is valid.
colorUtilities.isValidHslColor('hsl(123, 100%, 10%)') // => true
colorUtilities.isValidHslColor('hsl(255, 1%, 40%') // => true
colorUtilities.isValidHslColor('xhsl(255, 255, 255') // => false
colorUtilities.isValidHslColor('hsl(400, 50%, 50%') // => false
colorUtilities.isValidHslColor('hsl(100, 50, 50') // => false
colorUtilities.isValidHslColor(null) // => false
#splitHexColor(color: hexColor): hexColorValues
Splits the hexadecimal color into an array of it's hexadecimal RGB values.
color
- A hexadecimal color (eg. '#FFA500'). Shorthand notation ('#FFF')
and small caps are also supported. The return value is always normalized to uppercase two letters/numbers
long values.
The result of this method is a 3 items long array of hexadecimal RGB values.
colorUtilities.splitHexColor('#FFA500') // => ['FF', 'A5', '00']
colorUtilities.splitHexColor('#fff') // => ['FF', 'FF', 'FF']
#splitRgbColor(color: RGBColor): colorValues
Splits the RGB color into an array of it's RGB values.
color
- An RGB color (eg. 'rgb(255, 165, 0)').
The result of this method is a 3 items long array of RGB values.
colorUtilities.splitRgbColor('rgb(255, 165, 0)') // => [255, 165, 0]
colorUtilities.splitRgbColor('rgb(255, 255, 255)') // => [255, 255, 255]
#splitHslColor(color: HSLColor): colorValues
Splits the HSL color into an array of it's RGB values.
color
- An HSL color (eg. 'hsl(123, 10%, 50%)').
The result of this method is a 3 items long array of RGB values.
colorUtilities.splitHslColor('hsl(39, 100%, 50%)') // => [255, 166, 0]
colorUtilities.splitHslColor('hsl(0, 100%, 100%)') // => [255, 255, 255]
#normalizeHexColor(hexColor: hexColor): hexColor
Transforms a valid hex color into a uniform shape: 6 uppercased characters.
color
- A valid hex color that might be written in a shorthand notation or in lower case.
As a bonus, this method considers the colors without initial '#' as semi-valid and adds
the missing sign (validation still reports it's not a valid color).
A valid, 6 characters long (7 with '#') string of uppercased letters and numbers.
colorUtilities.normalizeHexColor('#fff') // => '#FFFFFF'
#normalizeRgbColor(rgbColor: RGBColor): RGBColor
Transforms a valid RGB color into a uniform shape (lowercased with consistent spacing).
color
- A valid RGB color.
A valid, lowercased color string with consistent spacing.
colorUtilities.normalizeRgbColor(' RGB ( 123, 123,1 )') // => 'rgb(123, 123, 1)
#normalizeHslColor(hslColor: HSLColor): HSLColor
Transforms a valid HSL color into a uniform shape (lowercased with consistent spacing).
color
- A valid HSL color.
A valid, lowercased color string with consistent spacing.
colorUtilities.normalizeHslColor(' HSL ( 123, 100%,1% )') // => 'hsl(123, 100%, 1%)
#calculateLuminanceOf(color: basicColor): number
Calculates the relative luminance of a color, as defined in the W3C Specification.
color
- A valid color.
A number representing the relative luminance in [0-1] range.
colorUtilities.calculateLuminanceOf('#FFFFFF') // => 1
colorUtilities.calculateLuminanceOf('rgb(255, 255, 255)') // => 1
colorUtilities.calculateLuminanceOf('hsl(100, 100%, 100%)') // => 1
colorUtilities.calculateLuminanceOf('#000000') // => 0
colorUtilities.calculateLuminanceOf('#FFA500') // => 0.48170267036309633
#calculateContrastRatio(color1: basicColor, color2: basicColor): number
Calculates a contrast ratio between two colors, as defined in the W3C Specification.
color1
- A valid color.
color2
- A valid color.
A contrast between the provided colors. The value is in range [1-21].
colorUtilities.calculateContrastRatio('#FFFFFF', '#000000') // => 21
colorUtilities.calculateContrastRatio('rgb(255, 255, 255)', 'rgb(0, 0, 0)') // => 21
colorUtilities.calculateContrastRatio('#000000', '#000000') // => 1
colorUtilities.calculateContrastRatio('#FFA500', '#000000') // => 11
colorUtilities.calculateContrastRatio('hsl(100, 100%, 100%)', '#000000') // => 21
An object designed to abstract the color's formatting to make it easier to work with in different scenarios.
The Color object can be created by using the factory method. While the typical class instantation
with a new
keyword is supported and is functionally equivalent, it's discouraged as not future proof. Whenever you can,
prever Color.create()
over new Color()
.
The creating method can optionally be provided with a raw color value (RGB, HEX, HSL) or with another Color object. In the second case, the provided object is returned. When used without any color, the default white is used.
const red = Color.create('#FF0000')
const green = Color.create('#00FF00')
const blue = Color.create(0, 0, 255)
// Discouraged, but functionally equivalent.
const black = new Color('#000')
The Color class exposes predefined basic colors that can be used instead of providing their values.
const black = Color.black
const white = Color.white
const red = Color.red
const green = Color.green
const blue = Color.blue
Color.create(color?: anyColor): Color
Creates a new color object (a factory method). Refer to the "Creation" section for more information.
Color.isColor(color?: anyColor): boolean
Tests if the provided color is a Color object.
color
- Color or an object to be tested.
A boolean informing if the object or color is a Color object.
Color.isColor(new Color('#FFFFFF') // => true
Color.isColor('#FFFFFF') // => false
Color object exposes properties about the color value and allow accessing different formatting of that color.
color.hex // => string: color as a hex
color.rgb // => string: color in the RGB format
color.hsl // => string: color in the HSL format
color.R // => number: red value of the color
color.G // => number: green value of the color
color.B // => number: blue value of the color
color.values // => number[]: array of the RGB values (eg. [255, 165, 0])
color.luminance // => number: the color's luminance
#set(color: anyColor): Color
Sets a new color value of the color, returning a new, modified color. This method doesn't actually modify the color it's invoked on. This method follows the same rules as the "Creation" section, except id doesn't support an empty call, meaning the desired color have to be provided.
color
- Color or a Color object to set.
A new Color object with the color being the one provided.
const black = Color.create('#000000')
const white = black.set('#FFFFFF')
black !== white // => true
#calculateContrastTo(color: anyColor): number
Calculates the contrast of the color compared to another one.
color
- Color or a Color object to which te comparison will be made.
A contract value.
const black = Color.create('#000000')
const white = Color.create('#FFFFFF')
blac.calculateContrastTo(white) // => 21
#equals(color: anyColor): boolean
Tests if the color object is of the same color as the tested one.
color
- Color or a Color object to test.
The test outcome as a boolean. true
means it's the same color.
const black = Color.create('#000000')
const white = Color.create('#FFFFFF')
black.equals(white) // => false
black.equals(new Color('#000000')) // => true
black.equals('#000000') // => true
#color.toString(): hexColor
Color object can be used in all places the string is expected. When concatenated with a string (or used in a template string), it's automatically transformed into it's hex representation. In other cases, the #toString() method transforms the object into hex.
A hex representation of the Color object.
const orange = Color.create('#FFA500')
"Color: " + orange // => "Color: #FFA500"
`I'm an orange with a value ${orange}` // => "I'm an orange with a value #FFA500"
color.toString() // => "#FFA500"
Since all color are essentaily strings, the library aliases them for more explicit typing. Those types are exported for consumers to use them together with the library or as a standalone definitions.
There are three basic color types:
hexColor
used for colors in the HEX format,rgbColor
used for colors in the RGB format,hexColor
used for colors in the HSL format.
There's also a basicColor
type representing all three possible types.
To use them, just import required definitions from the @kolory/color-utilities
module.
import {basicColor, hexColor, rgbColor, hslColor} from '@kolory/color-utilities'
const red: hexColor = '#FF0000'
const green: rgbColor = 'rgb(0, 255, 0)'
const blue: hslColor = 'hsl(240, 100%, 50%)'
let cameleon: basicColor
cameleon = '#FF0000'
cameleon = rgb(0, 255, 0)'
cameleon = 'hsl(240, 100%, 50%)'
In the internal calculations and raw colors management the library uses three value types representing values colors are consisted of.
hexValue
is used for a single value of the hex color (eg.A5
in#FFA500
),hexColorValues
is a triplet ofhexValue
s and represents all three values of a hex color (eg.['FF', 'A5', '00']
in#FFA500
),colorValues
is a triplet of the color's RGB values (eg.[255, 165, 0]
in#FFA500
).
import {colorValues, hexColorValues} from '@kolory/color-utilities'
const rawValuesOfOrange: colorValues = new ColorUtilities().parseColor('#FFA500')
const hexValuesOfOrange: hexColorValues = new ColorUtilities().splitHexColor('#FFA500')
When using the Color Object class that is not included in
any of the basic colors, the anyColor
type comes in handy. It's a union type of a basicColor
type ane the type
of the Color
class.
import {anyColor} from '@kolory/color-utilities'
let rainbow: anyColor
rainbow = '#FF0000'
rainbow = rgb(0, 255, 0)'
rainbow = 'hsl(240, 100%, 50%)'
rainbow = Color.create('#FFA500')
MIT