@@ -3,7 +3,7 @@ import type { CanvasManager } from 'features/controlLayers/konva/CanvasManager';
3
3
import { CanvasModuleBase } from 'features/controlLayers/konva/CanvasModuleBase' ;
4
4
import type { CanvasToolModule } from 'features/controlLayers/konva/CanvasTool/CanvasToolModule' ;
5
5
import { getColorAtCoordinate , getPrefixedId } from 'features/controlLayers/konva/util' ;
6
- import type { RgbColor } from 'features/controlLayers/store/types' ;
6
+ import type { RgbaColor } from 'features/controlLayers/store/types' ;
7
7
import { RGBA_BLACK } from 'features/controlLayers/store/types' ;
8
8
import Konva from 'konva' ;
9
9
import type { KonvaEventObject } from 'konva/lib/Node' ;
@@ -52,6 +52,39 @@ type CanvasColorPickerToolModuleConfig = {
52
52
* The color of the crosshair line borders.
53
53
*/
54
54
CROSSHAIR_BORDER_COLOR : string ;
55
+ /**
56
+ * The color of the RGBA value text.
57
+ */
58
+ TEXT_COLOR : string ;
59
+ /**
60
+ * The padding of the RGBA value text within the background rect.
61
+ */
62
+
63
+ TEXT_PADDING : number ;
64
+ /**
65
+ * The font size of the RGBA value text.
66
+ */
67
+ TEXT_FONT_SIZE : number ;
68
+ /**
69
+ * The color of the RGBA value text background rect.
70
+ */
71
+ TEXT_BG_COLOR : string ;
72
+ /**
73
+ * The width of the RGBA value text background rect.
74
+ */
75
+ TEXT_BG_WIDTH : number ;
76
+ /**
77
+ * The height of the RGBA value text background rect.
78
+ */
79
+ TEXT_BG_HEIGHT : number ;
80
+ /**
81
+ * The corner radius of the RGBA value text background rect.
82
+ */
83
+ TEXT_BG_CORNER_RADIUS : number ;
84
+ /**
85
+ * The x offset of the RGBA value text background rect from the color picker ring.
86
+ */
87
+ TEXT_BG_X_OFFSET : number ;
55
88
} ;
56
89
57
90
const DEFAULT_CONFIG : CanvasColorPickerToolModuleConfig = {
@@ -65,6 +98,14 @@ const DEFAULT_CONFIG: CanvasColorPickerToolModuleConfig = {
65
98
CROSSHAIR_LINE_LENGTH : 10 ,
66
99
CROSSHAIR_LINE_COLOR : 'rgba(0,0,0,1)' ,
67
100
CROSSHAIR_BORDER_COLOR : 'rgba(255,255,255,0.8)' ,
101
+ TEXT_COLOR : 'rgba(255,255,255,1)' ,
102
+ TEXT_BG_COLOR : 'rgba(0,0,0,0.8)' ,
103
+ TEXT_BG_HEIGHT : 62 ,
104
+ TEXT_BG_WIDTH : 62 ,
105
+ TEXT_BG_CORNER_RADIUS : 7 ,
106
+ TEXT_PADDING : 8 ,
107
+ TEXT_FONT_SIZE : 12 ,
108
+ TEXT_BG_X_OFFSET : 7 ,
68
109
} ;
69
110
70
111
/**
@@ -83,7 +124,7 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
83
124
/**
84
125
* The color currently under the cursor. Only has a value when the color picker tool is active.
85
126
*/
86
- $colorUnderCursor = atom < RgbColor > ( RGBA_BLACK ) ;
127
+ $colorUnderCursor = atom < RgbaColor > ( RGBA_BLACK ) ;
87
128
88
129
/**
89
130
* The Konva objects that make up the color picker tool preview:
@@ -105,6 +146,9 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
105
146
crosshairSouthOuter : Konva . Line ;
106
147
crosshairWestInner : Konva . Line ;
107
148
crosshairWestOuter : Konva . Line ;
149
+ rgbaTextGroup : Konva . Group ;
150
+ rgbaText : Konva . Text ;
151
+ rgbaTextBackground : Konva . Rect ;
108
152
} ;
109
153
110
154
constructor ( parent : CanvasToolModule ) {
@@ -202,8 +246,28 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
202
246
stroke : this . config . CROSSHAIR_BORDER_COLOR ,
203
247
perfectDrawEnabled : false ,
204
248
} ) ,
249
+ rgbaTextGroup : new Konva . Group ( {
250
+ listening : false ,
251
+ name : `${ this . type } :color_picker_text_group` ,
252
+ } ) ,
253
+ rgbaText : new Konva . Text ( {
254
+ listening : false ,
255
+ name : `${ this . type } :color_picker_text` ,
256
+ fill : this . config . TEXT_COLOR ,
257
+ fontFamily : 'monospace' ,
258
+ align : 'left' ,
259
+ fontStyle : 'bold' ,
260
+ verticalAlign : 'middle' ,
261
+ } ) ,
262
+ rgbaTextBackground : new Konva . Rect ( {
263
+ listening : false ,
264
+ name : `${ this . type } :color_picker_text_background` ,
265
+ fill : this . config . TEXT_BG_COLOR ,
266
+ } ) ,
205
267
} ;
206
268
269
+ this . konva . rgbaTextGroup . add ( this . konva . rgbaTextBackground , this . konva . rgbaText ) ;
270
+
207
271
this . konva . group . add (
208
272
this . konva . ringCandidateColor ,
209
273
this . konva . ringCurrentColor ,
@@ -216,7 +280,8 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
216
280
this . konva . crosshairSouthOuter ,
217
281
this . konva . crosshairSouthInner ,
218
282
this . konva . crosshairWestOuter ,
219
- this . konva . crosshairWestInner
283
+ this . konva . crosshairWestInner ,
284
+ this . konva . rgbaTextGroup
220
285
) ;
221
286
}
222
287
@@ -278,6 +343,24 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
278
343
outerRadius : colorPickerOuterRadius + twoPixels ,
279
344
} ) ;
280
345
346
+ const textBgWidth = this . manager . stage . unscale ( this . config . TEXT_BG_WIDTH ) ;
347
+ const textBgHeight = this . manager . stage . unscale ( this . config . TEXT_BG_HEIGHT ) ;
348
+
349
+ this . konva . rgbaTextBackground . setAttrs ( {
350
+ width : textBgWidth ,
351
+ height : textBgHeight ,
352
+ cornerRadius : this . manager . stage . unscale ( this . config . TEXT_BG_CORNER_RADIUS ) ,
353
+ } ) ;
354
+ this . konva . rgbaText . setAttrs ( {
355
+ padding : this . manager . stage . unscale ( this . config . TEXT_PADDING ) ,
356
+ fontSize : this . manager . stage . unscale ( this . config . TEXT_FONT_SIZE ) ,
357
+ text : `R: ${ colorUnderCursor . r } \nG: ${ colorUnderCursor . g } \nB: ${ colorUnderCursor . b } \nA: ${ colorUnderCursor . a } ` ,
358
+ } ) ;
359
+ this . konva . rgbaTextGroup . setAttrs ( {
360
+ x : x + this . manager . stage . unscale ( this . config . RING_OUTER_RADIUS + this . config . TEXT_BG_X_OFFSET ) ,
361
+ y : y - textBgHeight / 2 ,
362
+ } ) ;
363
+
281
364
const size = this . manager . stage . unscale ( this . config . CROSSHAIR_LINE_LENGTH ) ;
282
365
const space = this . manager . stage . unscale ( this . config . CROSSHAIR_INNER_RADIUS ) ;
283
366
const innerThickness = this . manager . stage . unscale ( this . config . CROSSHAIR_LINE_THICKNESS ) ;
@@ -324,11 +407,8 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
324
407
325
408
onStagePointerUp = ( _e : KonvaEventObject < PointerEvent > ) => {
326
409
const color = this . $colorUnderCursor . get ( ) ;
327
- if ( color ) {
328
- const settings = this . manager . stateApi . getSettings ( ) ;
329
- // This will update the color but not the alpha value
330
- this . manager . stateApi . setColor ( { ...settings . color , ...color } ) ;
331
- }
410
+ const settings = this . manager . stateApi . getSettings ( ) ;
411
+ this . manager . stateApi . setColor ( { ...settings . color , ...color } ) ;
332
412
} ;
333
413
334
414
onStagePointerMove = ( _e : KonvaEventObject < PointerEvent > ) => {
@@ -341,7 +421,11 @@ export class CanvasColorPickerToolModule extends CanvasModuleBase {
341
421
return ;
342
422
}
343
423
424
+ // Hide the background layer so we can get the color under the cursor without the grid interfering
425
+ this . manager . background . konva . layer . visible ( false ) ;
344
426
const color = getColorAtCoordinate ( this . manager . stage . konva . stage , cursorPos . absolute ) ;
427
+ this . manager . background . konva . layer . visible ( true ) ;
428
+
345
429
if ( color ) {
346
430
this . $colorUnderCursor . set ( color ) ;
347
431
}
0 commit comments