Class CSpace manages a three-parameter color value and transformation between different color models. It is a part of kch-rgbw-lib. See README.md for general information.
In TypeScript/ES2015:
import { CSpace, CSpaceTypes } from 'kch-rgbw-lib';
const rgb: CSpace = new CSpace('rgb', [0.8, 0, 0]);
const XYZ: CSpace = rgb.XYZ();
const a: number[] = XYZ.a;
console.log(a);
Currently the following color spaces are supported.
- HSV: Hue - saturation - value (brightness) in the range of [0, 360] for H, [0, 1] for S and V.
- RGB: Red - green - blue in the range of [0, 1]. sRGB is assumed in the chromaticity plane.
- XYZ: CIE 1931 XYZ color space.
- xyY: CIE 1931 xyY color space, which is a variation of the XYZ color space.
- xy: CIE 1931 xy chromaticity, which is identical to xyY color space except that Y term is not used.
The following transformations between these spaces are implemented.
- HSV <=> RGB : Equations from wikipedia.
- RGB <=> XYZ : With gamma correction. Equations from wikipedia.
- XYZ <=> xyY : (x, y) are obtained by divided by (X+Y+Z).
- xyY ==> xy : Y term is ignored in xy color space. It is unidirectional transformation.
The color spaces are connected this way:
HSV <=> RGB <=> XYZ <=> xyY => xy
Conversions are mathematically bidirectional and preservable (except xyY => xy), however, there is a cost of numerical errors. Experimentally, one transformation preserves value in the range of +/- 0.00001 to 0.001. The longest path, from HSV to xyY, can be numerically more erroneous.
export interface ICSpace {
type: CSpaceTypes;
a: number[];
};
export class CSpace implements ICSpace;
export interface ICSpaceR {
readonly type: CSpaceTypes;
readonly a: number[];
};
export class CSpaceR implements ICSpaceR;
Class CSpace
manages a colorspace.
Class CSpaceR
is a readonly version of CSpace
.
An empty CSpace is created. type
is set 'undefined'. You populate type
and a[]
.
Copy constructor.
A CSpace with given type
and arr[]
copied. arr[]
is examined
if it's acceptable in the same way .a
setter does.
EXPERIMENTAL: A CSpace is made from given waveLength
using an algorithm in wikipedia
then transformed to type
. It is marked as experimental, because it was found that
the equation gives large deviation from known values around green color.
Return a copy array of a[]. It's safe to modify or destroy the returned array.
Return type
.
Return corresponding value in .a[].
It's intended as (almost) toll-free access, where there is no type check except for .Y
.
You should use them when you are sure about the type.
Copy values from arr[]
. Length of arr[]
should be >= 3, except when type
is 'xy'.
Value of arr[]
is checked if it's in the proper range for the corresponding type
.
If it's not, it copies truncated value when possible.
When the length or value of arr[]
is not acceptable, it throws an exception.
When type
is 'xy', it behaves as
- Length of input
arr[]
>= 2 is accepted. - Only two values from
arr[]
copied..a[2]
is preserved.
Set the type
of this CSpace to given typ
.
Internally it uses the color transformation functions.
Set corresponding value to .a[].
It's intended as (almost) toll-free access, where there is no type check (except for .Y
). or value range check.
You should use them when you are sure about the type.
All member functions return this
so that you can chain them like this:
const x: CSpace = new CSpace(givenOne);
const a: number[] = x.rgb().a; // Chaining member functions
console.log(a);
Transform color space to the specified type and return this
.
When it cannot transform, it throws an exception.
If type
is 'undefined', it simply returns this
without doing anything.
.xy()
does not modify .a[2]
.
As explained above, not all combination of color spaces directly transformable. These transformation functions internally chain themselves to reach the output. Therefore, you don't need to do like this:
const x: CSpace = new CSpace('hsv', myArray);
const a: number[] = x.rgb().xyz().xyY().xy().a;
Transform color space to the specified type and return this
.
Copy value from
and return this
.
// Supported color spaces
type CSpaceTypes = { 'rgb' | 'hsv' | 'XYZ' | 'xyY' | 'xy' | undefined };
The MIT License (MIT) Copyright (c) K. Chinzei (kchinzei@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.