@osdlc/color 1.0.0 → 1.0.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 828fa0f: `compositeOver` clamps a finite source alpha to 0-1, so `a: 2` is treated as opaque and `a: -1` as transparent, as `withAlpha` does.
8
+
3
9
  ## 1.0.0
4
10
 
5
11
  ### Major Changes
package/README.md CHANGED
@@ -83,7 +83,8 @@ No function changes its arguments.
83
83
  - `fromPixel(px)` reads an `[r, g, b, a]` pixel from `ImageData.data`, where
84
84
  alpha is 0-255.
85
85
  - `compositeOver(src, ground)` paints `src` over an opaque `ground` and
86
- rounds each channel to 8 bits.
86
+ rounds each channel to 8 bits. A `src` alpha outside 0-1 is clamped. A `src`
87
+ with alpha exactly 1 is returned as is.
87
88
  - `relativeLuminance(color)` is the WCAG 2 relative luminance, 0-1.
88
89
  - `contrastRatio(a, b)` is the WCAG 2 contrast ratio, 1-21. Each side is an
89
90
  `Rgba` or a CSS string.
package/dist/index.d.ts CHANGED
@@ -14,8 +14,8 @@
14
14
  *
15
15
  * The fields are read-only, so a frozen object or an `as const` literal can be
16
16
  * passed in. Every function returns a new plain object, except that
17
- * {@link compositeOver} returns an opaque `src` as is; none changes its
18
- * arguments.
17
+ * {@link compositeOver} returns `src` itself when its alpha is exactly 1.
18
+ * None changes its arguments.
19
19
  */
20
20
  export interface Rgba {
21
21
  readonly r: number;
@@ -84,7 +84,9 @@ export declare function fromPixel(px: ArrayLike<number>): Rgba;
84
84
  * paints its own channels. Each mixed channel is rounded to 8 bits, as a
85
85
  * browser paints it. A Chromium 2D canvas gives the same pixel to within one
86
86
  * step per channel, because it also rounds the alpha to 8 bits and blends in
87
- * integers. An opaque `src` is returned as is.
87
+ * integers. A `src` whose alpha is exactly 1 is returned as is. A finite alpha
88
+ * outside 0-1 is clamped first, as {@link withAlpha} clamps it, so 2 gives a
89
+ * new object with `a: 1` and -1 gives the ground's channels.
88
90
  *
89
91
  * @throws RangeError when `ground.a` is below 1 or `NaN`. A translucent ground
90
92
  * shows what is under it, which this function does not know. Composite the
package/dist/index.js CHANGED
@@ -204,7 +204,9 @@ export function fromPixel(px) {
204
204
  * paints its own channels. Each mixed channel is rounded to 8 bits, as a
205
205
  * browser paints it. A Chromium 2D canvas gives the same pixel to within one
206
206
  * step per channel, because it also rounds the alpha to 8 bits and blends in
207
- * integers. An opaque `src` is returned as is.
207
+ * integers. A `src` whose alpha is exactly 1 is returned as is. A finite alpha
208
+ * outside 0-1 is clamped first, as {@link withAlpha} clamps it, so 2 gives a
209
+ * new object with `a: 1` and -1 gives the ground's channels.
208
210
  *
209
211
  * @throws RangeError when `ground.a` is below 1 or `NaN`. A translucent ground
210
212
  * shows what is under it, which this function does not know. Composite the
@@ -218,9 +220,12 @@ export function compositeOver(src, ground) {
218
220
  if (!Number.isFinite(src.a)) {
219
221
  throw new RangeError(`color: compositeOver needs a finite source alpha, got ${src.a}`);
220
222
  }
221
- if (src.a >= 1)
223
+ if (src.a === 1)
222
224
  return src;
223
- const mix = (s, g) => Math.round(s * src.a + g * (1 - src.a));
225
+ const a = clamp(src.a, 1);
226
+ if (a === 1)
227
+ return { r: src.r, g: src.g, b: src.b, a: 1 };
228
+ const mix = (s, g) => Math.round(s * a + g * (1 - a));
224
229
  return { r: mix(src.r, ground.r), g: mix(src.g, ground.g), b: mix(src.b, ground.b), a: 1 };
225
230
  }
226
231
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osdlc/color",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "sRGB colour arithmetic: parse the CSS colour syntaxes a browser reports, mix and composite in gamma-encoded sRGB, and measure WCAG 2 relative luminance and contrast. No dependencies.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": "sksizer",