@@ -144,7 +144,7 @@ export class UUIColorAreaElement extends LitElement {
144
144
// TODO: Can we move the parsing of a color string to shared utility function?
145
145
const parsed = colord ( newVal ) ;
146
146
147
- if ( parsed ) {
147
+ if ( parsed . isValid ( ) ) {
148
148
const { h, s, l } = parsed . toHsl ( ) ;
149
149
150
150
this . hue = h ;
@@ -228,43 +228,36 @@ export class UUIColorAreaElement extends LitElement {
228
228
) ;
229
229
}
230
230
231
- getValueFromMousePosition ( event : MouseEvent ) {
232
- return this . getValueFromCoordinates (
233
- event . clientX - event . offsetX ,
234
- event . clientY - event . offsetY
235
- ) ;
236
- }
237
-
238
- getValueFromTouchPosition ( event : TouchEvent ) {
239
- return this . getValueFromCoordinates (
240
- event . touches [ 0 ] . clientX ,
241
- event . touches [ 0 ] . clientY
242
- ) ;
243
- }
244
-
245
- getValueFromCoordinates ( x : number , y : number ) {
246
- const grid = this . shadowRoot ! . querySelector < HTMLElement > ( '.color-area' ) ! ;
247
- const { width, height } = grid . getBoundingClientRect ( ) ;
248
-
249
- this . saturation = clamp ( ( x / width ) * 100 , 0 , 100 ) ;
250
- this . lightness = clamp ( 100 - ( y / height ) * 100 , 0 , 100 ) ;
251
-
252
- this . syncValues ( ) ;
253
- }
254
-
255
231
syncValues ( ) {
256
232
const color = colord ( {
257
233
h : this . hue ,
258
234
s : this . saturation ,
259
235
l : this . lightness ,
260
- a : this . alpha ,
236
+ a : this . alpha / 100 ,
261
237
} ) ;
262
238
263
- this . value = color . toRgbString ( ) ;
239
+ this . _value = color . toRgbString ( ) ;
264
240
265
241
this . dispatchEvent ( new UUIColorAreaEvent ( UUIColorAreaEvent . CHANGE ) ) ;
266
242
}
267
243
244
+ /** Generates a hex string from HSL values. Hue must be 0-360. All other arguments must be 0-100. */
245
+ private getHexString (
246
+ hue : number ,
247
+ saturation : number ,
248
+ lightness : number ,
249
+ alpha = 100
250
+ ) {
251
+ const color = colord (
252
+ `hsla(${ hue } , ${ saturation } %, ${ lightness } %, ${ alpha / 100 } )`
253
+ ) ;
254
+ if ( ! color . isValid ( ) ) {
255
+ return '' ;
256
+ }
257
+
258
+ return color . toHex ( ) ;
259
+ }
260
+
268
261
render ( ) {
269
262
const gridHandleX = this . saturation ;
270
263
const gridHandleY = 100 - this . brightness ;
@@ -273,7 +266,9 @@ export class UUIColorAreaElement extends LitElement {
273
266
< div
274
267
part ="grid "
275
268
class ="color-area "
276
- style =${ styleMap ( { backgroundColor : `hsl(${ this . hue } deg, 100%, 50%)` } ) }
269
+ style =${ styleMap ( {
270
+ backgroundColor : this . getHexString ( this . hue , 100 , 50 ) ,
271
+ } ) }
277
272
@mousedown =${ this . handleGridDrag }
278
273
@touchstart=${ this . handleGridDrag } >
279
274
< span
@@ -285,7 +280,12 @@ export class UUIColorAreaElement extends LitElement {
285
280
style =${ styleMap ( {
286
281
top : `${ gridHandleY } %` ,
287
282
left : `${ gridHandleX } %` ,
288
- backgroundColor : `hsla(${ this . hue } deg, ${ this . saturation } %, ${ this . lightness } %)` ,
283
+ backgroundColor : this . getHexString (
284
+ this . hue ,
285
+ this . saturation ,
286
+ this . lightness ,
287
+ this . alpha
288
+ ) ,
289
289
} ) }
290
290
role="application"
291
291
tabindex=${ ifDefined ( this . disabled ? undefined : '0' ) }
0 commit comments