@rokkit/core 1.4.0 → 1.4.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/color-space.js +25 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/core",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Contains core utility functions and classes that can be used in various components.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -46,6 +46,6 @@
46
46
  "ramda": "^0.32.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@rokkit/icons": "1.4.0"
49
+ "@rokkit/icons": "1.4.1"
50
50
  }
51
51
  }
@@ -77,6 +77,16 @@ function oklchLuminance(L, C, H) {
77
77
  return matMul(LMS_TO_XYZ, lms)[1]
78
78
  }
79
79
 
80
+ // ── Value-shape regexes ─────────────────────────────────────────────────
81
+ // Declared above their first use (relativeLuminance) rather than beside the
82
+ // class that also consumes them, and named rather than inlined so the three
83
+ // bare-channel dialects can be told apart at a glance.
84
+
85
+ const HEX_RE = /^#[0-9a-fA-F]{6}$/
86
+ const BARE_OKLCH_RE = /^[\d.]+\s+[\d.]+\s+[\d.]+$/
87
+ const BARE_RGB_COMMA_RE = /^\d+\s*,\s*\d+\s*,\s*\d+$/
88
+ const BARE_RGB_SPACE_RE = /^\d+\s+\d+\s+\d+$/
89
+
80
90
  /**
81
91
  * Relative luminance (Y, 0–1) of a palette value in the given color space.
82
92
  * Accepts hex (`#rrggbb`), bare RGB (`"r,g,b"` / `"r g b"`), and bare OKLCH
@@ -94,16 +104,27 @@ export function relativeLuminance(value, space = 'rgb') {
94
104
  const [r, g, b] = parseHex(v)
95
105
  return rgbLuminance(r, g, b)
96
106
  }
97
- if (space === 'oklch' && /^[\d.]+\s+[\d.]+\s+[\d.]+$/.test(v)) {
107
+ // OKLCH is checked before bare RGB and only when asked for: "0.6 0.1 40" is a
108
+ // valid shape for both dialects, and `space` is the only thing that says which.
109
+ if (space === 'oklch' && BARE_OKLCH_RE.test(v)) {
98
110
  const [L, C, H] = v.split(/\s+/).map(Number)
99
111
  return oklchLuminance(L, C, H)
100
112
  }
101
- if (/^\d+\s*,\s*\d+\s*,\s*\d+$/.test(v)) {
113
+ return bareRgbLuminance(v)
114
+ }
115
+
116
+ /**
117
+ * Luminance of a bare `"r,g,b"` or `"r g b"` triple, or null when `v` is neither.
118
+ * @param {string} v
119
+ * @returns {number | null}
120
+ */
121
+ function bareRgbLuminance(v) {
122
+ if (BARE_RGB_COMMA_RE.test(v)) {
102
123
  const [r, g, b] = v.split(',').map((s) => Number(s.trim()))
103
124
  return rgbLuminance(r, g, b)
104
125
  }
105
- if (/^\d+\s+\d+\s+\d+$/.test(v)) {
106
- const [r, g, b] = v.trim().split(/\s+/).map(Number)
126
+ if (BARE_RGB_SPACE_RE.test(v)) {
127
+ const [r, g, b] = v.split(/\s+/).map(Number)
107
128
  return rgbLuminance(r, g, b)
108
129
  }
109
130
  return null
@@ -176,10 +197,6 @@ function hexToOklch(hex) {
176
197
  return `${Lr} ${Cr} ${Hr}`
177
198
  }
178
199
 
179
- // ── Hex regex ───────────────────────────────────────────────────────────
180
-
181
- const HEX_RE = /^#[0-9a-fA-F]{6}$/
182
-
183
200
  // ── Base class ──────────────────────────────────────────────────────────
184
201
 
185
202
  /**