@oscarpalmer/atoms 0.40.0 → 0.41.0

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/dist/js/colour.js CHANGED
@@ -17,16 +17,16 @@ var createHex = function(original) {
17
17
  return hexToRgb(value);
18
18
  },
19
19
  toString() {
20
- return value;
20
+ return `#${value}`;
21
21
  }
22
22
  });
23
23
  Object.defineProperty(instance, "value", {
24
24
  get() {
25
- return value.slice();
25
+ return `#${value}`;
26
26
  },
27
27
  set(hex) {
28
28
  if (anyPattern.test(hex)) {
29
- value = `#${getHex(hex)}`;
29
+ value = getNormalisedHex(hex);
30
30
  }
31
31
  }
32
32
  });
@@ -96,12 +96,15 @@ function getForegroundColour(value) {
96
96
  const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
97
97
  return luminance > 0.625 ? "black" : "white";
98
98
  }
99
- var getHex = function(value) {
99
+ function getHexColour(value) {
100
+ return createHex(anyPattern.test(value) ? getNormalisedHex(value) : "000000");
101
+ }
102
+ var getNormalisedHex = function(value) {
100
103
  const normalised = value.replace(/^#/, "");
101
104
  return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
102
105
  };
103
106
  function hexToRgb(value) {
104
- const hex = anyPattern.test(value) ? getHex(value) : "";
107
+ const hex = anyPattern.test(value) ? getNormalisedHex(value) : "";
105
108
  const pairs = groupedPattern.exec(hex) ?? [];
106
109
  const rgb = [];
107
110
  const { length } = pairs;
@@ -129,7 +132,7 @@ function hslToRgb(value) {
129
132
  });
130
133
  }
131
134
  function rgbToHex(value) {
132
- return createHex(`#${[value.red, value.green, value.blue].map((colour) => {
135
+ return createHex(`${[value.red, value.green, value.blue].map((colour) => {
133
136
  const hex = colour.toString(16);
134
137
  return hex.length === 1 ? `0${hex}` : hex;
135
138
  }).join("")}`);
@@ -181,5 +184,6 @@ export {
181
184
  rgbToHex,
182
185
  hslToRgb,
183
186
  hexToRgb,
187
+ getHexColour,
184
188
  getForegroundColour
185
189
  };
@@ -10,16 +10,16 @@ var createHex = function(original) {
10
10
  return hexToRgb(value);
11
11
  },
12
12
  toString() {
13
- return value;
13
+ return `#${value}`;
14
14
  }
15
15
  });
16
16
  Object.defineProperty(instance, "value", {
17
17
  get() {
18
- return value.slice();
18
+ return `#${value}`;
19
19
  },
20
20
  set(hex) {
21
21
  if (anyPattern.test(hex)) {
22
- value = `#${getHex(hex)}`;
22
+ value = getNormalisedHex(hex);
23
23
  }
24
24
  }
25
25
  });
@@ -89,12 +89,15 @@ function getForegroundColour(value) {
89
89
  const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
90
90
  return luminance > 0.625 ? "black" : "white";
91
91
  }
92
- var getHex = function(value) {
92
+ function getHexColour(value) {
93
+ return createHex(anyPattern.test(value) ? getNormalisedHex(value) : "000000");
94
+ }
95
+ var getNormalisedHex = function(value) {
93
96
  const normalised = value.replace(/^#/, "");
94
97
  return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
95
98
  };
96
99
  function hexToRgb(value) {
97
- const hex = anyPattern.test(value) ? getHex(value) : "";
100
+ const hex = anyPattern.test(value) ? getNormalisedHex(value) : "";
98
101
  const pairs = groupedPattern.exec(hex) ?? [];
99
102
  const rgb = [];
100
103
  const { length } = pairs;
@@ -122,7 +125,7 @@ function hslToRgb(value) {
122
125
  });
123
126
  }
124
127
  function rgbToHex(value) {
125
- return createHex(`#${[value.red, value.green, value.blue].map((colour) => {
128
+ return createHex(`${[value.red, value.green, value.blue].map((colour) => {
126
129
  const hex = colour.toString(16);
127
130
  return hex.length === 1 ? `0${hex}` : hex;
128
131
  }).join("")}`);
@@ -174,5 +177,6 @@ export {
174
177
  rgbToHex,
175
178
  hslToRgb,
176
179
  hexToRgb,
180
+ getHexColour,
177
181
  getForegroundColour
178
182
  };
package/dist/js/index.js CHANGED
@@ -181,16 +181,16 @@ var createHex = function(original) {
181
181
  return hexToRgb(value);
182
182
  },
183
183
  toString() {
184
- return value;
184
+ return `#${value}`;
185
185
  }
186
186
  });
187
187
  Object.defineProperty(instance, "value", {
188
188
  get() {
189
- return value.slice();
189
+ return `#${value}`;
190
190
  },
191
191
  set(hex) {
192
192
  if (anyPattern.test(hex)) {
193
- value = `#${getHex(hex)}`;
193
+ value = getNormalisedHex(hex);
194
194
  }
195
195
  }
196
196
  });
@@ -260,12 +260,15 @@ function getForegroundColour(value) {
260
260
  const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
261
261
  return luminance > 0.625 ? "black" : "white";
262
262
  }
263
- var getHex = function(value) {
263
+ function getHexColour(value) {
264
+ return createHex(anyPattern.test(value) ? getNormalisedHex(value) : "000000");
265
+ }
266
+ var getNormalisedHex = function(value) {
264
267
  const normalised = value.replace(/^#/, "");
265
268
  return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
266
269
  };
267
270
  function hexToRgb(value) {
268
- const hex = anyPattern.test(value) ? getHex(value) : "";
271
+ const hex = anyPattern.test(value) ? getNormalisedHex(value) : "";
269
272
  const pairs = groupedPattern.exec(hex) ?? [];
270
273
  const rgb = [];
271
274
  const { length } = pairs;
@@ -293,7 +296,7 @@ function hslToRgb(value) {
293
296
  });
294
297
  }
295
298
  function rgbToHex(value) {
296
- return createHex(`#${[value.red, value.green, value.blue].map((colour) => {
299
+ return createHex(`${[value.red, value.green, value.blue].map((colour) => {
297
300
  const hex = colour.toString(16);
298
301
  return hex.length === 1 ? `0${hex}` : hex;
299
302
  }).join("")}`);
@@ -1089,6 +1092,7 @@ export {
1089
1092
  getRandomBoolean,
1090
1093
  getPosition,
1091
1094
  getNumber,
1095
+ getHexColour,
1092
1096
  getForegroundColour,
1093
1097
  getFocusableElements,
1094
1098
  getElementUnderPointer,
package/package.json CHANGED
@@ -130,5 +130,5 @@
130
130
  },
131
131
  "type": "module",
132
132
  "types": "./types/index.d.ts",
133
- "version": "0.40.0"
133
+ "version": "0.41.0"
134
134
  }
package/src/js/colour.ts CHANGED
@@ -103,17 +103,17 @@ function createHex(original: string): HexColour {
103
103
  return hexToRgb(value);
104
104
  },
105
105
  toString() {
106
- return value;
106
+ return `#${value}`;
107
107
  },
108
108
  });
109
109
 
110
110
  Object.defineProperty(instance, 'value', {
111
111
  get() {
112
- return value.slice();
112
+ return `#${value}`;
113
113
  },
114
114
  set(hex: string) {
115
115
  if (anyPattern.test(hex)) {
116
- value = `#${getHex(hex)}`;
116
+ value = getNormalisedHex(hex);
117
117
  }
118
118
  },
119
119
  });
@@ -208,7 +208,14 @@ export function getForegroundColour(value: RGBColourValue): string {
208
208
  return luminance > 0.625 ? 'black' : 'white';
209
209
  }
210
210
 
211
- function getHex(value: string): string {
211
+ /**
212
+ * Get a hex-colour from a string
213
+ */
214
+ export function getHexColour(value: string): HexColour {
215
+ return createHex(anyPattern.test(value) ? getNormalisedHex(value) : '000000');
216
+ }
217
+
218
+ function getNormalisedHex(value: string): string {
212
219
  const normalised = value.replace(/^#/, '');
213
220
 
214
221
  return normalised.length === 3
@@ -223,7 +230,7 @@ function getHex(value: string): string {
223
230
  * Convert a hex-colour to an RGB-colour
224
231
  */
225
232
  export function hexToRgb(value: string): RGBColour {
226
- const hex = anyPattern.test(value) ? getHex(value) : '';
233
+ const hex = anyPattern.test(value) ? getNormalisedHex(value) : '';
227
234
  const pairs = groupedPattern.exec(hex) ?? [];
228
235
  const rgb = [];
229
236
 
@@ -269,7 +276,7 @@ export function hslToRgb(value: HSLColourValue): RGBColour {
269
276
  */
270
277
  export function rgbToHex(value: RGBColourValue): HexColour {
271
278
  return createHex(
272
- `#${[value.red, value.green, value.blue]
279
+ `${[value.red, value.green, value.blue]
273
280
  .map(colour => {
274
281
  const hex = colour.toString(16);
275
282
 
package/types/colour.d.ts CHANGED
@@ -80,6 +80,10 @@ export type RGBColourValue = {
80
80
  * Get a foreground colour _(usually text)_ based on a background colour's luminance
81
81
  */
82
82
  export declare function getForegroundColour(value: RGBColourValue): string;
83
+ /**
84
+ * Get a hex-colour from a string
85
+ */
86
+ export declare function getHexColour(value: string): HexColour;
83
87
  /**
84
88
  * Convert a hex-colour to an RGB-colour
85
89
  */