Skip to content

Commit

Permalink
feat: rename RGBBytes to Bytes
Browse files Browse the repository at this point in the history
- keep old API as deprecated functions
- use bytes in docs and tests

Closes #22
  • Loading branch information
dmnsgn committed Feb 5, 2024
1 parent c876db9 commit b71c0b4
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 90 deletions.
90 changes: 45 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ API naming follows the following rules:
## Functions

<dl>
<dt><a href="#fromBytes">fromBytes(color, bytes)</a> ⇒ <code><a href="#color">color</a></code></dt>
<dd><p>Updates a color based on byte values.</p>
</dd>
<dt><a href="#toBytes">toBytes(color, out)</a> ⇒ <code><a href="#bytes">bytes</a></code></dt>
<dd><p>Get RGB[A] color components as bytes array.</p>
</dd>
<dt><a href="#create">create([r], [g], [b], [a])</a> ⇒ <code><a href="#color">color</a></code></dt>
<dd><p>Creates a new color from linear values.</p>
</dd>
Expand Down Expand Up @@ -178,12 +184,6 @@ API naming follows the following rules:
<dt><a href="#toRGB">toRGB(color, out)</a> ⇒ <code><a href="#color">color</a></code></dt>
<dd><p>Returns a copy of a RGB color.</p>
</dd>
<dt><a href="#fromRGBBytes">fromRGBBytes(color, bytes)</a> ⇒ <code><a href="#color">color</a></code></dt>
<dd><p>Updates a color based on byte values.</p>
</dd>
<dt><a href="#toRGBBytes">toRGBBytes(color, out)</a> ⇒ <code><a href="#bytes">bytes</a></code></dt>
<dd><p>Get RGB[A] color components as bytes array.</p>
</dd>
<dt><a href="#fromXYZ">fromXYZ(color, x, y, z, a)</a> ⇒ <code><a href="#color">color</a></code></dt>
<dd><p>Updates a color based on XYZ values and alpha.</p>
</dd>
Expand All @@ -195,6 +195,10 @@ API naming follows the following rules:
## Typedefs

<dl>
<dt><a href="#bytes">bytes</a> : <code>Array.&lt;number&gt;</code></dt>
<dd><p>An array of 3 (RGB) or 4 (A) values in bytes.</p>
<p>All components in the range 0 &lt;= x &lt;= 255</p>
</dd>
<dt><a href="#color">color</a> : <code>Array.&lt;number&gt;</code></dt>
<dd><p>An array of 3 (RGB) or 4 (A) values.</p>
<p>All components in the range 0 &lt;= x &lt;= 1</p>
Expand Down Expand Up @@ -248,10 +252,6 @@ API naming follows the following rules:
<dd><p>Cartesian form using D65 standard illuminant.</p>
<p>Components range: 0 &lt;= l &lt;= 1; -0.233 &lt;= a &lt;= 0.276; -0.311 &lt;= b &lt;= 0.198;</p>
</dd>
<dt><a href="#bytes">bytes</a> : <code>Array.&lt;number&gt;</code></dt>
<dd><p>An array of 3 (RGB) or 4 (A) values in bytes.</p>
<p>All components in the range 0 &lt;= x &lt;= 255</p>
</dd>
<dt><a href="#xyz">xyz</a> : <code>Array.&lt;number&gt;</code></dt>
<dd><p>CIE XYZ using D65 standard illuminant.</p>
<p>Components range: 0 &lt;= x &lt;= 0.95; 0 &lt;= y &lt;= 1; 0 &lt;= z &lt;= 1.08;</p>
Expand Down Expand Up @@ -290,6 +290,32 @@ Convert component to linear value
| ----- | ------------------- |
| c | <code>number</code> |

<a name="fromBytes"></a>

## fromBytes(color, bytes) ⇒ [<code>color</code>](#color)

Updates a color based on byte values.

**Kind**: global function

| Param | Type |
| ----- | ---------------------------- |
| color | [<code>color</code>](#color) |
| bytes | [<code>bytes</code>](#bytes) |

<a name="toBytes"></a>

## toBytes(color, out) ⇒ [<code>bytes</code>](#bytes)

Get RGB[A] color components as bytes array.

**Kind**: global function

| Param | Type |
| ----- | ---------------------------- |
| color | [<code>color</code>](#color) |
| out | <code>Array</code> |

<a name="create"></a>

## create([r], [g], [b], [a]) ⇒ [<code>color</code>](#color)
Expand Down Expand Up @@ -771,32 +797,6 @@ Returns a copy of a RGB color.
| color | [<code>color</code>](#color) |
| out | <code>Array</code> |

<a name="fromRGBBytes"></a>

## fromRGBBytes(color, bytes) ⇒ [<code>color</code>](#color)

Updates a color based on byte values.

**Kind**: global function

| Param | Type |
| ----- | ---------------------------- |
| color | [<code>color</code>](#color) |
| bytes | [<code>bytes</code>](#bytes) |

<a name="toRGBBytes"></a>

## toRGBBytes(color, out) ⇒ [<code>bytes</code>](#bytes)

Get RGB[A] color components as bytes array.

**Kind**: global function

| Param | Type |
| ----- | ---------------------------- |
| color | [<code>color</code>](#color) |
| out | <code>Array</code> |

<a name="fromXYZ"></a>

## fromXYZ(color, x, y, z, a) ⇒ [<code>color</code>](#color)
Expand Down Expand Up @@ -826,6 +826,15 @@ Returns a XYZ representation of a given color.
| color | [<code>color</code>](#color) |
| out | <code>Array</code> |

<a name="bytes"></a>

## bytes : <code>Array.&lt;number&gt;</code>

An array of 3 (RGB) or 4 (A) values in bytes.

All components in the range 0 <= x <= 255

**Kind**: global typedef
<a name="color"></a>

## color : <code>Array.&lt;number&gt;</code>
Expand Down Expand Up @@ -957,15 +966,6 @@ Components range: 0 <= l <= 1; -0.233 <= a <= 0.276; -0.311 <= b <= 0.198;

**Kind**: global typedef
**See**: [https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab](https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab)
<a name="bytes"></a>

## bytes : <code>Array.&lt;number&gt;</code>

An array of 3 (RGB) or 4 (A) values in bytes.

All components in the range 0 <= x <= 255

**Kind**: global typedef
<a name="xyz"></a>

## xyz : <code>Array.&lt;number&gt;</code>
Expand Down
22 changes: 20 additions & 2 deletions rgbbytes.js → bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param {bytes} bytes
* @returns {import("./color.js").color}
*/
export function fromRGBBytes(color, [r, g, b, a]) {
export function fromBytes(color, [r, g, b, a]) {
color[0] = r / 255;
color[1] = g / 255;
color[2] = b / 255;
Expand All @@ -24,10 +24,28 @@ export function fromRGBBytes(color, [r, g, b, a]) {
* @param {Array} out
* @returns {bytes}
*/
export function toRGBBytes(color, out = []) {
export function toBytes(color, out = []) {
out[0] = Math.round(color[0] * 255);
out[1] = Math.round(color[1] * 255);
out[2] = Math.round(color[2] * 255);
if (color[3] !== undefined) out[3] = Math.round(color[3] * 255);
return out;
}

/**
* @deprecated Use "fromBytes()".
* @ignore
*/
export function fromRGBBytes(color, bytes) {
console.error(`"fromRGBBytes()" deprecated. Use "fromBytes()".`);
return fromBytes(color, bytes);
}

/**
* @deprecated Use "toBytes()".
* @ignore
*/
export function toRGBBytes(color, out) {
console.error(`"toRGBBytes()" deprecated. Use "toBytes()".`);
return toBytes(color, out);
}
4 changes: 2 additions & 2 deletions css.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { toHSL } from "./hsl.js";
import { toHWB } from "./hwb.js";
import { toLab, D50 } from "./lab.js";
import { toLCHuv } from "./lchuv.js";
import { toRGBBytes } from "./rgbbytes.js";
import { toBytes } from "./bytes.js";
import { TMP, floorArray } from "./utils.js";

/**
Expand All @@ -18,7 +18,7 @@ import { TMP, floorArray } from "./utils.js";
* @returns {css}
*/
export function toCSSRGB(color, precision = 5) {
toRGBBytes(color, TMP);
toBytes(color, TMP);
if (precision !== undefined) floorArray(TMP, precision);
const a = color[3] !== undefined ? `, ${color[3]}` : "";
return `rgb${a ? "a" : ""}(${TMP.slice(0, 3).join(", ")}${a})`;
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./color.js";

export * from "./rgb.js";
export * from "./rgbbytes.js";
export * from "./bytes.js";
export * from "./linear.js";

export * from "./hex.js";
Expand Down
Loading

0 comments on commit b71c0b4

Please sign in to comment.