From 06cc1e200ecd6070d4944afba286aefc9b6092fe Mon Sep 17 00:00:00 2001 From: Damien Seguin Date: Mon, 5 Feb 2024 13:28:05 +0000 Subject: [PATCH] feat: deprecate toRGB (for set()) and fromRGB (for fromValues) Closes #21 --- README.md | 54 +++++++++++-------------------- color.js | 38 ++++++++++++++++++++-- index.js | 1 - package-lock.json | 4 +-- package.json | 4 +-- rgb.js | 31 ------------------ test/index.js | 81 +++++++++++++++++++++++++++++++---------------- 7 files changed, 113 insertions(+), 100 deletions(-) delete mode 100644 rgb.js diff --git a/README.md b/README.md index 260aaba..33ac803 100755 --- a/README.md +++ b/README.md @@ -91,6 +91,9 @@ API naming follows the following rules:
set(color, color2)color

Sets a color to another color.

+
fromValues(color, r, g, b, [a])color
+

Updates a color based on r, g, b, [a] values.

+
toCSSRGB(color, [precision])css

Returns a rgb CSS string representation of a given color.

@@ -178,12 +181,6 @@ API naming follows the following rules:
toOklab(color, out)oklab

Returns an Oklab representation of a given color.

-
fromRGB(color, r, g, b, [a])color
-

Updates a color based on r, g, b, a values.

-
-
toRGB(color, out)color
-

Returns a copy of a RGB color.

-
fromXYZ(color, x, y, z, a)color

Updates a color based on XYZ values and alpha.

@@ -356,6 +353,22 @@ Sets a color to another color. | color | [color](#color) | | color2 | [color](#color) | + + +## fromValues(color, r, g, b, [a]) ⇒ [color](#color) + +Updates a color based on r, g, b, [a] values. + +**Kind**: global function + +| Param | Type | +| ----- | ---------------------------- | +| color | [color](#color) | +| r | number | +| g | number | +| b | number | +| [a] | number | + ## toCSSRGB(color, [precision]) ⇒ [css](#css) @@ -768,35 +781,6 @@ Returns an Oklab representation of a given color. | color | [color](#color) | | out | Array | - - -## fromRGB(color, r, g, b, [a]) ⇒ [color](#color) - -Updates a color based on r, g, b, a values. - -**Kind**: global function - -| Param | Type | -| ----- | ---------------------------- | -| color | [color](#color) | -| r | number | -| g | number | -| b | number | -| [a] | number | - - - -## toRGB(color, out) ⇒ [color](#color) - -Returns a copy of a RGB color. - -**Kind**: global function - -| Param | Type | -| ----- | ---------------------------- | -| color | [color](#color) | -| out | Array | - ## fromXYZ(color, x, y, z, a) ⇒ [color](#color) diff --git a/color.js b/color.js index 2c9e0e7..3ff8440 100644 --- a/color.js +++ b/color.js @@ -1,3 +1,4 @@ +import { setAlpha } from "./utils.js"; /** * @typedef {number[]} color An array of 3 (RGB) or 4 (A) values. * @@ -35,6 +36,39 @@ export function set(color, color2) { color[0] = color2[0]; color[1] = color2[1]; color[2] = color2[2]; - if (color2[3] !== undefined) color[3] = color2[3]; - return color; + return setAlpha(color, color2[3]); +} + +/** + * Updates a color based on r, g, b, [a] values. + * @param {import("./color.js").color} color + * @param {number} r + * @param {number} g + * @param {number} b + * @param {number} [a] + * @returns {import("./color.js").color} + */ +export function fromValues(color, r, g, b, a) { + color[0] = r; + color[1] = g; + color[2] = b; + return setAlpha(color, a); +} + +/** + * @deprecated Use "fromValues()". + * @ignore + */ +export function fromRGB(color, r, g, b, a) { + console.error(`"fromRGB()" deprecated. Use "fromValues()".`); + return fromValues(color, r, g, b, a); +} + +/** + * @deprecated Use "set()". + * @ignore + */ +export function toRGB(color, out = []) { + console.error(`"toRGB()" deprecated. Use "set()".`); + return set(out, color); } diff --git a/index.js b/index.js index ec918f0..888bead 100755 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ export * from "./color.js"; -export * from "./rgb.js"; export * from "./bytes.js"; export * from "./linear.js"; diff --git a/package-lock.json b/package-lock.json index 1005d9f..fc8e2a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,8 @@ "version": "2.0.4", "license": "MIT", "engines": { - "node": ">=18.0.0", - "npm": ">=8.6.0" + "node": ">=20.0.0", + "npm": ">=9.6.4" } } } diff --git a/package.json b/package.json index cab37b6..3ac5a70 100755 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "test:watch": "node --test --watch test" }, "engines": { - "node": ">=18.0.0", - "npm": ">=8.6.0" + "node": ">=20.0.0", + "npm": ">=9.6.4" } } diff --git a/rgb.js b/rgb.js deleted file mode 100644 index 305a18d..0000000 --- a/rgb.js +++ /dev/null @@ -1,31 +0,0 @@ -import { setAlpha } from "./utils.js"; - -/** - * Updates a color based on r, g, b, a values. - * @param {import("./color.js").color} color - * @param {number} r - * @param {number} g - * @param {number} b - * @param {number} [a] - * @returns {import("./color.js").color} - */ -export function fromRGB(color, r, g, b, a) { - color[0] = r; - color[1] = g; - color[2] = b; - setAlpha(color, a); - return color; -} - -/** - * Returns a copy of a RGB color. - * @param {import("./color.js").color} color - * @param {Array} out - * @returns {import("./color.js").color} - */ -export function toRGB([r, g, b, a], out = []) { - out[0] = r; - out[1] = g; - out[2] = b; - return setAlpha(out, a); -} diff --git a/test/index.js b/test/index.js index 0fe4880..9811d98 100644 --- a/test/index.js +++ b/test/index.js @@ -40,12 +40,14 @@ describe("copy()", () => { describe("set()", () => { it("should set a color", () => { deepEqual(color.set(TEMP_VEC3, [0.1, 0.2, 0.3]), [0.1, 0.2, 0.3]); + deepEqual(color.toRGB([0.1, 0.2, 0.3]), [0.1, 0.2, 0.3]); }); it("should set a color with supplied alpha", () => { deepEqual( color.set(color.create(), [0.1, 0.2, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4], ); + deepEqual(color.toRGB([0.1, 0.2, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4]); }); it("should set a color and keep the color alpha", () => { deepEqual(color.set(color.create(), [0.1, 0.2, 0.3]), [ @@ -57,34 +59,34 @@ describe("set()", () => { }); }); -describe("RGB", () => { - describe("from", () => { - it("should set a color", () => { - deepEqual(color.fromRGB(TEMP_VEC3, 0.1, 0.2, 0.3), [0.1, 0.2, 0.3]); - }); - it("should set a color with supplied alpha", () => { - deepEqual( - color.fromRGB(color.create(), 0.1, 0.2, 0.3, 0.4), - [0.1, 0.2, 0.3, 0.4], - ); - }); - it("should set a color and keep the color alpha", () => { - deepEqual(color.fromRGB(color.create(), 0.1, 0.2, 0.3), [ - 0.1, - 0.2, - 0.3, - DEFAULT_ALPHA, - ]); - }); +describe("fromValues", () => { + it("should set a color", () => { + deepEqual(color.fromValues(TEMP_VEC3, 0.1, 0.2, 0.3), [0.1, 0.2, 0.3]); + deepEqual(color.fromRGB(TEMP_VEC3, 0.1, 0.2, 0.3), [0.1, 0.2, 0.3]); }); - - describe("to", () => { - it("should return a color copy with alpha from a color with supplied alpha", () => { - deepEqual(color.toRGB([0.1, 0.2, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4]); - }); - it("should return a color copy from a color", () => { - deepEqual(color.toRGB([0.1, 0.2, 0.3]), [0.1, 0.2, 0.3]); - }); + it("should set a color with supplied alpha", () => { + deepEqual( + color.fromValues(color.create(), 0.1, 0.2, 0.3, 0.4), + [0.1, 0.2, 0.3, 0.4], + ); + deepEqual( + color.fromRGB(color.create(), 0.1, 0.2, 0.3, 0.4), + [0.1, 0.2, 0.3, 0.4], + ); + }); + it("should set a color and keep the color alpha", () => { + deepEqual(color.fromValues(color.create(), 0.1, 0.2, 0.3), [ + 0.1, + 0.2, + 0.3, + DEFAULT_ALPHA, + ]); + deepEqual(color.fromRGB(color.create(), 0.1, 0.2, 0.3), [ + 0.1, + 0.2, + 0.3, + DEFAULT_ALPHA, + ]); }); }); @@ -96,6 +98,11 @@ describe("Bytes", () => { 100 / 255, 125 / 255, ]); + deepEqual(color.fromRGBBytes(TEMP_VEC3, [222, 100, 125]), [ + 222 / 255, + 100 / 255, + 125 / 255, + ]); }); it("should set a color from a Bytes array with supplied alpha", () => { deepEqual(color.fromBytes(color.create(), [222, 100, 125, 23]), [ @@ -104,6 +111,12 @@ describe("Bytes", () => { 125 / 255, 23 / 255, ]); + deepEqual(color.fromRGBBytes(color.create(), [222, 100, 125, 23]), [ + 222 / 255, + 100 / 255, + 125 / 255, + 23 / 255, + ]); }); it("should set a color from a Bytes array and keep the color alpha", () => { deepEqual(color.fromBytes(color.create(), [222, 100, 125]), [ @@ -112,6 +125,12 @@ describe("Bytes", () => { 125 / 255, DEFAULT_ALPHA, ]); + deepEqual(color.fromRGBBytes(color.create(), [222, 100, 125]), [ + 222 / 255, + 100 / 255, + 125 / 255, + DEFAULT_ALPHA, + ]); }); }); @@ -121,12 +140,20 @@ describe("Bytes", () => { color.toBytes([222 / 255, 100 / 255, 125 / 255, 23 / 255]), [222, 100, 125, 23], ); + deepEqual( + color.toRGBBytes([222 / 255, 100 / 255, 125 / 255, 23 / 255]), + [222, 100, 125, 23], + ); }); it("should return a RGB Bytes array from a color", () => { deepEqual( color.toBytes([222 / 255, 100 / 255, 125 / 255]), [222, 100, 125], ); + deepEqual( + color.toRGBBytes([222 / 255, 100 / 255, 125 / 255]), + [222, 100, 125], + ); }); }); });