@texel/color 1.1.3 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,11 @@ import {
8
8
 
9
9
  import { sRGBGammaToLinearVec3, sRGBLinearToGammaVec3 } from "./util.js";
10
10
 
11
+ /**
12
+ * The sRGB color space in linear form, without a transfer function, aliased as <code>"srgb-linear"</code>.
13
+ * @type {ColorSpace}
14
+ * @category spaces
15
+ */
11
16
  export const sRGBLinear = {
12
17
  id: "srgb-linear",
13
18
  toXYZ_M: linear_sRGB_to_XYZ_M,
@@ -16,6 +21,11 @@ export const sRGBLinear = {
16
21
  fromLMS_M: LMS_to_linear_sRGB_M,
17
22
  };
18
23
 
24
+ /**
25
+ * The sRGB color space, with a transfer function, aliased as <code>"srgb"</code>. Inherits from the {@link sRGBLinear} color space.
26
+ * @type {ColorSpace}
27
+ * @category spaces
28
+ */
19
29
  export const sRGB = {
20
30
  id: "srgb",
21
31
  base: sRGBLinear,
@@ -23,6 +33,11 @@ export const sRGB = {
23
33
  fromBase: sRGBLinearToGammaVec3,
24
34
  };
25
35
 
36
+ /**
37
+ * A color gamut for the {@link sRGB} color space.
38
+ * @type {ColorGamut}
39
+ * @category gamuts
40
+ */
26
41
  export const sRGBGamut = {
27
42
  space: sRGB,
28
43
  coefficients: OKLab_to_linear_sRGB_coefficients,
@@ -1,5 +1,12 @@
1
1
  import { vec3 } from "../util.js";
2
2
 
3
+ /**
4
+ * Converts a single sRGB gamma-corrected channel value to linear light (un-companded) form.
5
+ * @param {number} val - The sRGB gamma-corrected channel value in the range [0, 1].
6
+ * @returns {number} The linear light channel value.
7
+ * @method
8
+ * @category rgb
9
+ */
3
10
  export const sRGBGammaToLinear = (val) => {
4
11
  // convert a single channel value
5
12
  // where in-gamut values are in the range [0 - 1]
@@ -15,6 +22,13 @@ export const sRGBGammaToLinear = (val) => {
15
22
  : sign * Math.pow((abs + 0.055) / 1.055, 2.4);
16
23
  };
17
24
 
25
+ /**
26
+ * Converts a single linear-light channel value to sRGB gamma-corrected form.
27
+ * @param {number} val - The linear-light channel value in the range [0, 1].
28
+ * @returns {number} The sRGB gamma-corrected channel value.
29
+ * @method
30
+ * @category rgb
31
+ */
18
32
  export const sRGBLinearToGamma = (val) => {
19
33
  // convert a single channel linear-light value in range 0-1
20
34
  // to gamma corrected form
package/src/spaces/xyz.js CHANGED
@@ -28,20 +28,44 @@ export const D50_to_D65_M = [
28
28
  [0.012314014864481998, -0.020507649298898964, 1.330365926242124],
29
29
  ];
30
30
 
31
+ /**
32
+ * Converts a color from XYZ with D65 whitepoint to XYZ with D50 whitepoint.
33
+ * @method
34
+ * @category xyz
35
+ * @param {Vector} XYZ - The input color in XYZ with D65 whitepoint.
36
+ * @param {Vector} [out=vec3()] - The output color in XYZ with D50 whitepoint.
37
+ * @returns {Vector} The converted color in XYZ with D50 whitepoint.
38
+ */
31
39
  export const XYZD65ToD50 = (XYZ, out = vec3()) =>
32
40
  transform(XYZ, D65_to_D50_M, out);
33
41
 
42
+ /**
43
+ * Converts a color from XYZ with D50 whitepoint to XYZ with D65 whitepoint.
44
+ * @method
45
+ * @category xyz
46
+ * @param {Vector} XYZ - The input color in XYZ with D50 whitepoint.
47
+ * @param {Vector} [out=vec3()] - The output color in XYZ with D65 whitepoint.
48
+ * @returns {Vector} The converted color in XYZ with D65 whitepoint.
49
+ */
34
50
  export const XYZD50ToD65 = (XYZ, out = vec3()) =>
35
51
  transform(XYZ, D50_to_D65_M, out);
36
52
 
37
- // XYZ using D65 whitepoint
53
+ /**
54
+ * XYZ color space with D65 whitepoint, aliased as <code>"xyz"</code>.
55
+ * @type {ColorSpace}
56
+ * @category spaces
57
+ */
38
58
  export const XYZ = {
39
59
  id: "xyz", // xyz-d65
40
60
  toLMS_M: XYZ_to_LMS_M,
41
61
  fromLMS_M: LMS_to_XYZ_M,
42
62
  };
43
63
 
44
- // XYZ using D50 whitepoint
64
+ /**
65
+ * XYZ color space with D50 whitepoint, aliased as <code>"xyz-d50"</code>.
66
+ * @type {ColorSpace}
67
+ * @category spaces
68
+ */
45
69
  export const XYZD50 = {
46
70
  id: "xyz-d50",
47
71
  base: XYZ,
package/src/spaces.js CHANGED
@@ -18,6 +18,13 @@ export * from "./spaces/rec2020.js";
18
18
  export * from "./spaces/a98-rgb.js";
19
19
  export * from "./spaces/prophoto-rgb.js";
20
20
 
21
+ /**
22
+ * Returns a list of color spaces.
23
+ *
24
+ * @method
25
+ * @returns {ColorSpace[]} An array of color space objects.
26
+ * @category core
27
+ */
21
28
  export const listColorSpaces = () => {
22
29
  return [
23
30
  XYZ, // D65
@@ -39,6 +46,13 @@ export const listColorSpaces = () => {
39
46
  ];
40
47
  };
41
48
 
49
+ /**
50
+ * Returns a list of color gamuts.
51
+ *
52
+ * @method
53
+ * @returns {ColorGamut[]} An array of color gamut objects.
54
+ * @category core
55
+ */
42
56
  export const listColorGamuts = () => {
43
57
  return [sRGBGamut, DisplayP3Gamut, Rec2020Gamut, A98RGBGamut];
44
58
  };
package/src/util.js CHANGED
@@ -1,15 +1,60 @@
1
- const GAMUT_EPSILON = 0.000075;
2
-
1
+ /**
2
+ * Clamps a value between a minimum and maximum value.
3
+ * @method
4
+ * @param {number} value The value to clamp.
5
+ * @param {number} min The minimum value.
6
+ * @param {number} max The maximum value.
7
+ * @returns {number} The clamped value.
8
+ * @category utils
9
+ */
3
10
  export const clamp = (value, min, max) => Math.max(Math.min(value, max), min);
4
11
 
12
+ /**
13
+ * Linearly interpolates between two values.
14
+ * @method
15
+ * @param {number} min The start value.
16
+ * @param {number} max The end value.
17
+ * @param {number} t The interpolation factor between 0 and 1.
18
+ * @returns {number} The interpolated value.
19
+ * @category utils
20
+ */
5
21
  export const lerp = (min, max, t) => min * (1 - t) + max * t;
6
22
 
23
+ /**
24
+ * Converts degrees to radians.
25
+ * @method
26
+ * @param {number} n The angle in degrees.
27
+ * @returns {number} The angle in radians.
28
+ * @category utils
29
+ */
7
30
  export const degToRad = (n) => (n * Math.PI) / 180;
8
31
 
32
+ /**
33
+ * Converts radians to degrees.
34
+ * @method
35
+ * @param {number} n The angle in radians.
36
+ * @returns {number} The angle in degrees.
37
+ * @category utils
38
+ */
9
39
  export const radToDeg = (n) => (n * 180) / Math.PI;
10
40
 
41
+ /**
42
+ * Constrains an angle to the range [0, 360).
43
+ * @method
44
+ * @param {number} angle The angle in degrees.
45
+ * @returns {number} The constrained angle.
46
+ * @category utils
47
+ */
11
48
  export const constrainAngle = (angle) => ((angle % 360) + 360) % 360;
12
49
 
50
+ /**
51
+ * Converts a hex color string to an RGB array.
52
+ * @method
53
+ * @param {string} str The hex color string.
54
+ * @param {Vector} [out=vec3()] The output array.
55
+ * @returns {Vector} The RGB array.
56
+ * @category rgb
57
+ */
13
58
  export const hexToRGB = (str, out = vec3()) => {
14
59
  let hex = str.replace(/#/, "");
15
60
  if (hex.length === 3) {
@@ -26,12 +71,31 @@ export const hexToRGB = (str, out = vec3()) => {
26
71
  return out;
27
72
  };
28
73
 
74
+ /**
75
+ * Converts an RGB array to a hex color string.
76
+ * @method
77
+ * @param {Vector} rgb The RGB array.
78
+ * @returns {string} The hex color string.
79
+ * @category rgb
80
+ */
29
81
  export const RGBToHex = (rgb) =>
30
82
  `#${rgb.map((n) => floatToByte(n).toString(16).padStart(2, "0")).join("")}`;
31
83
 
32
- /** @deprecated use RGBToHex */
84
+ /**
85
+ * @method
86
+ * @deprecated Use RGBToHex instead.
87
+ * @category rgb
88
+ */
33
89
  export const RGBtoHex = RGBToHex;
34
90
 
91
+ /**
92
+ * Checks if an RGB color is within the gamut.
93
+ * @method
94
+ * @param {Vector} lrgb The linear RGB array.
95
+ * @param {number} [ep=GAMUT_EPSILON] The epsilon value for comparison.
96
+ * @returns {boolean} True if the color is within the gamut, false otherwise.
97
+ * @category rgb
98
+ */
35
99
  export const isRGBInGamut = (lrgb, ep = GAMUT_EPSILON) => {
36
100
  const r = lrgb[0];
37
101
  const g = lrgb[1];
@@ -46,6 +110,14 @@ export const isRGBInGamut = (lrgb, ep = GAMUT_EPSILON) => {
46
110
  );
47
111
  };
48
112
 
113
+ /**
114
+ * Clamps an RGB array to the range [0, 1].
115
+ * @method
116
+ * @param {Vector} rgb The RGB array.
117
+ * @param {Vector} [out=vec3()] The output array.
118
+ * @returns {Vector} The clamped RGB array.
119
+ * @category rgb
120
+ */
49
121
  export const clampedRGB = (rgb, out = vec3()) => {
50
122
  out[0] = clamp(rgb[0], 0, 1);
51
123
  out[1] = clamp(rgb[1], 0, 1);
@@ -53,6 +125,14 @@ export const clampedRGB = (rgb, out = vec3()) => {
53
125
  return out;
54
126
  };
55
127
 
128
+ /**
129
+ * Converts xyY color space to XYZ color space.
130
+ * @method
131
+ * @param {Vector} arg The xyY array.
132
+ * @param {Vector} [out=vec3()] The output array.
133
+ * @returns {Vector} The XYZ array.
134
+ * @category xyz
135
+ */
56
136
  export const xyY_to_XYZ = (arg, out = vec3()) => {
57
137
  let X, Y, Z, x, y;
58
138
  x = arg[0];
@@ -70,6 +150,14 @@ export const xyY_to_XYZ = (arg, out = vec3()) => {
70
150
  return out;
71
151
  };
72
152
 
153
+ /**
154
+ * Converts XYZ color space to xyY color space.
155
+ * @method
156
+ * @param {Vector} arg The XYZ array.
157
+ * @param {Vector} [out=vec3()] The output array.
158
+ * @returns {Vector} The xyY array.
159
+ * @category xyz
160
+ */
73
161
  export const XYZ_to_xyY = (arg, out = vec3()) => {
74
162
  let sum, X, Y, Z;
75
163
  X = arg[0];
@@ -87,29 +175,43 @@ export const XYZ_to_xyY = (arg, out = vec3()) => {
87
175
  return out;
88
176
  };
89
177
 
178
+ /**
179
+ * Converts a float value to a byte value.
180
+ * @method
181
+ * @param {number} n The float value.
182
+ * @returns {number} The byte value.
183
+ * @category utils
184
+ */
90
185
  export const floatToByte = (n) => clamp(Math.round(255 * n), 0, 255);
91
186
 
92
- // Undocumented
93
-
187
+ /**
188
+ * Creates a new vec3 array.
189
+ * @method
190
+ * @returns {Vector} The vec3 array.
191
+ * @category utils
192
+ */
94
193
  export const vec3 = () => [0, 0, 0];
95
194
 
96
- // export const normalizeHue = (hue) => ((hue = hue % 360) < 0 ? hue + 360 : hue);
97
-
98
- // in degrees
99
- // export const angle_delta = (angle1, angle2) => {
100
- // const diff = ((angle2 - angle1 + 180) % 360) - 180;
101
- // return diff < -180 ? diff + 360 : diff;
102
- // };
103
-
104
- // function shortAngleDistRad(a0, a1) {
105
- // var max = Math.PI * 2;
106
- // var da = (a1 - a0) % max;
107
- // return ((2 * da) % max) - da;
108
- // }
109
-
195
+ /**
196
+ * Calculates the delta angle between two angles.
197
+ * @method
198
+ * @param {number} a0 The first angle in degrees.
199
+ * @param {number} a1 The second angle in degrees.
200
+ * @returns {number} The delta angle in degrees.
201
+ * @category utils
202
+ */
110
203
  export const deltaAngle = (a0, a1) => {
111
204
  var da = (a1 - a0) % 360;
112
205
  return ((2 * da) % 360) - da;
113
206
  };
114
207
 
208
+ /**
209
+ * Linearly interpolates between two angles.
210
+ * @method
211
+ * @param {number} a0 The start angle in degrees.
212
+ * @param {number} a1 The end angle in degrees.
213
+ * @param {number} t The interpolation factor between 0 and 1.
214
+ * @returns {number} The interpolated angle in degrees.
215
+ * @category utils
216
+ */
115
217
  export const lerpAngle = (a0, a1, t) => a0 + deltaAngle(a0, a1) * t;