1- import  math
1+ import  std / [ math, parseutils] 
22import  strutils
33
44import  view
1515  ColorPickerPalette *  {.pure .} =  enum 
1616    HSV 
1717
18-   ColorComponent *  {.pure .} =  enum 
19-     H
20-     S
21-     V
22- 
2318  ColorView *  =  ref  object  of  View 
2419    # # Color quad that reacts to outer world
2520    main: bool   # # Defines if view is main or from history
3631  ColorPickerV *  =  ref  object  of  View 
3732    # # Value tuning widget
3833
39-   ColorComponentTextField  =  ref  object  of  TextField 
40-     cComponent: ColorComponent 
41- 
4234  ColorPickerView *  =  ref  object  of  Control 
4335    # # Complex Widget that allows to pick color using HSV palette
4436    palette:     ColorPickerPalette   # # Palette (RGB, HSV, HSL, etc.)
@@ -139,33 +131,12 @@ method draw(cph: ColorPickerH, r: Rect) =
139131
140132proc  colorHasChanged (cpv: ColorPickerView ) = 
141133  # # Perform update of ColorPickerView components
142-   cpv.tfH.text =  formatFloat (cpv.currentColor.h, precision  =  3 )
143-   cpv.tfS.text =  formatFloat (cpv.currentColor.s, precision  =  3 )
144-   cpv.tfV.text =  formatFloat (cpv.currentColor.v, precision  =  3 )
134+   cpv.tfH.text =  formatFloat (cpv.currentColor.h, ffDecimal,  3 )
135+   cpv.tfS.text =  formatFloat (cpv.currentColor.s, ffDecimal,  3 )
136+   cpv.tfV.text =  formatFloat (cpv.currentColor.v, ffDecimal,  3 )
145137  cpv.chosenColorView.backgroundColor =  hsvToRGB (cpv.currentColor)
146138  cpv.setNeedsDisplay ()
147139
148- method  onTextInput (ccf: ColorComponentTextField , s: string ): bool  = 
149-   discard  procCall  ccf.TextField .onTextInput (s)
150- 
151-   let  val =  try : parseFloat (ccf.text)
152-         except  Exception : - 100.0 
153-   if  val ==  - 100.0 : return  true 
154- 
155-   let  cpv =  ccf.enclosingColorPickerView ()
156- 
157-   case  ccf.cComponent
158-   of  ColorComponent .H:
159-     cpv.currentColor.h =  val
160-   of  ColorComponent .S:
161-     cpv.currentColor.s =  val
162-   of  ColorComponent .V:
163-     cpv.currentColor.v =  val
164- 
165-   cpv.colorHasChanged ()
166- 
167-   return  true 
168- 
169140method  onTouchEv (cph: ColorPickerH , e: var  Event ): bool  {.gcsafe .}= 
170141  let  cpv =  cph.enclosingColorPickerView ()
171142
@@ -338,6 +309,15 @@ method onTouchEv(cv: ColorView, e: var Event): bool =
338309
339310  return  true 
340311
312+ proc  updateColorFromTextField (c: ColorPickerView , tf: TextField , value: var  float ) = 
313+   let  t =  tf.text
314+   if  parseFloat (t, value) !=  t.len:
315+     value =  - 100 
316+   let  curp =  tf.cursorPosition
317+   c.colorHasChanged ()
318+   tf.text =  t
319+   tf.cursorPosition =  curp
320+ 
341321method  init * (cpv: ColorPickerView ) = 
342322  #  Basic Properties Initialization
343323  procCall  cpv.View .init ()
@@ -356,12 +336,14 @@ method init*(cpv: ColorPickerView) =
356336      bottom ==  labelS.layout.vars.top -  margin
357337      text: " H: " 
358338
359-     -  ColorComponentTextField  as  tfH:
339+     -  TextField  as  tfH:
360340      leading ==  prev.trailing +  margin
361341      width ==  60 
362342      y ==  prev
363343      height ==  prev
364-       cComponent: ColorComponent .H
344+       continuous: true 
345+       onAction:
346+         updateColorFromTextField (cpv, tFH, cpv.currentColor.h)
365347
366348    -  ColorPickerH  as  cpH:
367349      leading ==  prev.trailing +  margin
@@ -375,12 +357,14 @@ method init*(cpv: ColorPickerView) =
375357      bottom ==  labelV.layout.vars.top -  margin
376358      text: " S: " 
377359
378-     -  ColorComponentTextField  as  tfS:
360+     -  TextField  as  tfS:
379361      leading ==  prev.trailing +  margin
380362      width ==  tfH.layout.vars.width
381363      y ==  prev
382364      height ==  prev
383-       cComponent: ColorComponent .S
365+       continuous: true 
366+       onAction:
367+         updateColorFromTextField (cpv, tFS, cpv.currentColor.s)
384368
385369    -  ColorPickerS  as  cpS:
386370      leading ==  prev.trailing +  margin
@@ -394,12 +378,14 @@ method init*(cpv: ColorPickerView) =
394378      bottom ==  super -  margin
395379      text: " V: " 
396380
397-     -  ColorComponentTextField  as  tfV:
381+     -  TextField  as  tfV:
398382      leading ==  prev.trailing +  margin
399383      width ==  tfH.layout.vars.width
400384      y ==  prev
401385      height ==  prev
402-       cComponent: ColorComponent .V
386+       continuous: true 
387+       onAction:
388+         updateColorFromTextField (cpv, tFV, cpv.currentColor.v)
403389
404390    -  ColorPickerV  as  cpB:
405391      leading ==  prev.trailing +  margin
0 commit comments