Miscellaneous Methods
getType()
.getType(): string
This method returns a string of the type matching the class name. This can be used in cases where the type is not known but using type checking is troublesome.
Example Usage
let c1: Color.rgb = Color.from('rgb', [255, 0, 255])
console.log(c1.getType())
// rgb
let c2: Color.kelvin = Color.from('kelvin', 1500)
console.log(c2.getType())
// kelvin
css()
.css({ method: string }): string
Will format string for direct usage in CSS. Conversion is automatic. Methods available: hex
, rgb
, rgba
, hsl
, and hsla
.
Hex will include the hash #
prefix and does not yet support alpha.
The precision of the alpha channel is approximately 1/256, or ~0.004, at best. The precision output, therefore, is limited to 4 decimal places.
Example Usage
console.log(Color.from('rgb', [255, 0, 255]).css())
console.log(Color.from('rgb', [255, 0, 255]).css({ method: 'hex' }))
// #ff00ff
console.log(Color.from('rgb', [255, 0, 255]).css({ method: 'rgb' }))
// rgb(255, 0, 255)
console.log(Color.from('rgb', [255, 0, 255]).css({ method: 'rgba' }))
// rgba(255, 0, 255, 1)
console.log(Color.from('rgb', [255, 0, 255, 128]).css({ method: 'rgba' }))
// rgba(255, 0, 255, 0.5)
console.log(Color.from('hsv', [300, 100, 100, 25]).css({ method: 'hsl' }))
// hsl(300, 100%, 50%)
console.log(Color.from('hsv', [300, 100, 100, 12.3456]).css({ method: 'hsla' }))
// hsla(300, 100%, 50%, 0.1235)
toString()
.toString(whitespace?: number|string, quotes: boolean = false, showType: boolean = true): string
whitespace
This is the equivalent space
parameter for JSON.stringify(value, replacer, space)
and will, if given, will be usedd to insert white space (including indentation, line break characters, etc) into the output JSON string for legibility.
If a Number, it indicates the number of space characters to use as white space for indenting purposes; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used.
If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space.
If this parameter is not provided (or is null), no white space is used.
quotes
A boolean parameter used to denote whether quotes should be stripped from the output.
showType
A boolean parameter used to denote whether the type should appear at the front of the JSON.
Example Usage
console.log(Color.from('rgb', [255, 0, 255]).toString())
// rgb:{"r":255,"g":0,"b":255,"a":255,"bitDepth":8}
console.log(Color.from('rgb', [255, 0, 255]).toString(2))
// rgb: {
// "r": 255,
// "g": 0,
// "b": 255,
// "a": 255,
// "bitDepth": 8
// }
console.log(Color.from('rgb', [255, 0, 255]).toString(2, false))
// rgb: {
// r: 255,
// g: 0,
// b: 255,
// a: 255,
// bitDepth: 8
// }
console.log(Color.from('rgb', [255, 0, 255]).toString(null, false, false))
// {r:255,g:0,b:255,a:255,bitDepth:8}