# Anyone got a great idea on how to draw the HSV color space FAST

**URL:** <https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369>\
**Category:** Other\
**Created:** [7 July 2023 00:09 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369 "2023-07-07T00:09:51Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [7 July 2023 00:09 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/1 "2023-07-07T00:09:51Z")

</div>

Basically I need to make something that is sort of like the Color picker because - well I use an IDE that DOESNT have “Insert Color” as one of its tools (go figure)

So I quickly write a Xojo app (yeah yeah) and had it do RGB colors and automatically just puts whatever color you change the slider to on the clipboard so its “select color go back to other IDE & paste”

That works dandy

Then I though “Gee I should make an HSV picker as well since its a lt more visual in terms of the colors etc.”

And the ever famous “How hard can that be ?”

Well the initial code to draw the color wheel - icky but decently quick  
And even after hand tweaking code & moving things out of the loops I could etc its still not fast  
Even with compiling using agressive - not super fast  
Fast enough to not be totally annoying but not as fast was it might be

So the quest is a way, on macOS to draw using HSV colors the color wheel  
I have two sliders on the page

```auto
sliderAlphaHSV live scrolling on min = 0 max = 255
sliderValue live scrolling on min = 0 max = 100

```

Any takers on making this code super fast ?  
the one parameter it takes is a picture created as

```auto
    p = new picture( 200, 200)

```

everything else _should_ be local  
tried to make it faster by drawing 8 points each pass but not sure that helped or not

```auto
Protected Sub DrawHSV(p as picture)
  #Pragma NilObjectChecking False
  #Pragma BackgroundTasks False
  #Pragma BoundsChecking False
  #Pragma StackOverflowChecking False
  
  Dim g As graphics = p.graphics
  
  g.ClearRect 0, 0, g.width, g.height
  
  Const drawLabels = False
  
  Const Pi = 3.14159
  Const maxDegrees = 45
  
  Dim toRadValue As Double = pi/180
  
  Dim maxRadius As Integer = (g.width/2) - (.1 * (g.width/2) )
  
  Dim xpos As Double = g.width / 2
  Dim ypos As Double = g.height / 2
  
  Dim alphaValue As Integer = sliderAlphaHSV.value
  Dim colorValue As Double = sliderValue.value / sliderValue.Maximum
  
  For saturation As Integer = 0 To maxRadius
    
    Dim osize As Double = Max(1, .1 * saturation)
    
    Dim satValue As Double = saturation / maxRadius
    
    For hue As Integer = 0 To maxDegrees
      
      Dim tmpHueQ1a As Double = hue
      Dim tmpHueQ1b As Double = tmpHueQ1a + 45
      Dim tmpHueQ2a As Double = tmpHueQ1b + 45
      Dim tmpHueQ2b As Double = tmpHueQ2a + 45
      Dim tmpHueQ3a As Double = tmpHueQ2b + 45
      Dim tmpHueQ3b As Double = tmpHueQ3a + 45
      Dim tmpHueQ4a As Double = tmpHueQ3b + 45
      Dim tmpHueQ4b As Double = tmpHueQ4a + 45
      
      Dim xoffs As Double
      Dim yoffs As Double
      
      xoffs = Cos(tmpHueQ1a*toRadValue) * saturation
      yoffs = Sin(tmpHueQ1a*toRadValue) * saturation
      
      ' |
      ' |
      ' ----|----
      ' | 1a
      ' | 
      g.ForeColor = HSV( tmpHueQ1a / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos + xoffs, ypos + yOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ1a), xpos + xoffs, ypos + yOffs
      End If
      
      ' |
      ' |
      ' ----|----
      ' |   
      ' 2a| 
      g.ForeColor = HSV( tmpHueQ2a / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos - yoffs, ypos + xOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ2a), xpos - yoffs, ypos + xOffs
      End If
      
      ' |
      ' 3a |
      ' ----|----
      ' |   
      ' | 
      g.ForeColor = HSV( tmpHueQ3a / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos - xoffs, ypos - yOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ3a), xpos - xoffs, ypos - yOffs
      End If
      
      ' |4a
      ' |
      ' ----|----
      ' |  
      ' | 
      g.ForeColor = HSV( tmpHueQ4a / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos + yoffs, ypos - xOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ4a), xpos + yoffs, ypos - xOffs
      End If
      
      ' |
      ' |
      ' ----|----
      ' |   
      ' |1b 
      xoffs = Cos(tmpHueQ1b*pi/180) * saturation
      yoffs = Sin(tmpHueQ1b*pi/180) * saturation
      
      
      g.ForeColor = HSV( tmpHueQ1b / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos + xoffs, ypos + yOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ1b), xpos + xoffs, ypos + yOffs
      End If
      
      ' |
      ' |
      ' ----|----
      ' 2b |   
      ' |
      g.ForeColor = HSV( tmpHueQ2b / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos - yoffs, ypos + xOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ2b), xpos - yoffs, ypos + xOffs
      End If
      
      ' 3b|
      ' |
      ' ----|----
      ' |   
      ' |
      g.ForeColor = HSV( tmpHueQ3b / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos - xoffs, ypos - yOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ3b), xpos - xoffs, ypos - yOffs
      End If
      
      ' |
      ' | 4b 
      ' ----|----
      ' |   
      ' |
      g.ForeColor = HSV( tmpHueQ4b / 360 , satValue , colorValue, alphaValue )
      g.FillOval xpos + yoffs, ypos - xOffs , osize, osize
      If drawLabels And saturation = maxRadius Then
         g.forecolor = &cffffff
         g.DrawString Str(tmpHueQ4b), xpos + yoffs, ypos - xOffs
      End If
      
    Next
  Next
  
  
End Sub

```

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [7 July 2023 02:33 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/2 "2023-07-07T02:33:11Z")

</div>

I just use this one.

> **[‎System Color Picker](https://apps.apple.com/us/app/system-color-picker/id1545870783?mt=12)**
>
> ‎Pick colors from anywhere using the built-in color picker.
> 
> 
> ■ Features
> 
> \- Quickly copy, paste, and convert colors in Hex, HSL, RGB, LCH format
> \- Show as a normal app or in the menu bar
> \- Pick a color or toggle the window from anywhere with a global...

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [7 July 2023 03:40 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/3 "2023-07-07T03:40:21Z")

</div>

Yeah I looked at several before I decided to do this at all  
Esp since I need something on “those other platforms” for this same damned IDE  
Go figure

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [7 July 2023 07:01 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/4 "2023-07-07T07:01:12Z")

</div>

![grafik](https://ifnotnil.com/uploads/default/original/2X/e/ed7498856c6696ed4d88d5c74b7aafe9bc993981.png)  
A Color-Picker is a needed thing for a UI Editor

Something Xojo should also have (let’s say it is a MUST have) is a multi-mode Color Picker. I didn’t get why it is only a rudimentary solution. Netbeans Matisse for example had even one like the shown in 2003. There is another functional leak for the Xojo Gang. All we need for Design would be the right way to build a Designer. Like to have Layout Manager and Color-Selection Model and so on. QT has it in it’s visual Designer, Matisse has it, JFormdesigner also. Microsoft left the path of visual GUI builders completely (why ever they have done it). But there is also a big amount of problems building a visual Designer IDE. That’s the foundation for building instead of a visual Designer a declarative UI language and let the users build their UI’s there.  
So there is also a chance to use external picker solutions and paste the resulting code. As more people using declarative UI (Xaml for example) there is no visual designer provided for the languages in the future. One example for a different approach ist for example JavaFX. You have a declarative UI in JavafX with their fxml language and on top the Scenebuilder as a drag and drop WYSIWYG gui editor. So JavaFX has both approaches for GUI development. Declarative and graphical editor. I don’t get why Microsoft is not going this way also. Dotnet would be much more interesting for many users when and ih there would be a technology like this behind.

One of Xojo’s goodies is the UI editor which has a good usability. Except missed color picker feature. Something Java for example has. I didn’t get why there is no implementation in their IDE to have that. But this kind of decisions are made from the Master of enginering in austin. So there aalso never will be one.

---

<div class="post-metadata">

**Author:** ![Jeannot](https://ifnotnil.com/user_avatar/ifnotnil.com/jeannot/32/3787_2.png) [@Jeannot](https://ifnotnil.com/u/Jeannot)\
**Post date:** [7 July 2023 08:33 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/5 "2023-07-07T08:33:24Z")

</div>

> [@thorstenstueker](#):
>
> One of Xojo’s goodies is the UI editor which has a good usability. Except missed color picker feature. Something Java for example has. **I didn’t get why there is no implementation in their IDE** to have that. But this kind of decisions are made from the Master of enginering in austin. So there aalso never will be one.

I don’t get that one. Perhaps I’ve overlooked something. They have one (well a bad one, as they take the rudimentary OS one), but they have one. A bit freaky that you have to use again events to get the value back, but overall it works “somehow”, obviously not in the way Napalm needs it.

 ![Screenshot 2023-07-07 at 10.21.44](https://ifnotnil.com/uploads/default/original/2X/c/c0c7dd443af67338b33b3e50abe2403ba20b7828.jpeg)

 ![Screenshot 2023-07-07 at 10.22.16](https://ifnotnil.com/uploads/default/original/2X/6/638e029535fcf12badcb6002888b91122cdafbf5.jpeg)

Having started Xojo after a couple of months, I can only shake my head. How could I possibly have worked with such a slow IDE for so long …

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [7 July 2023 08:47 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/6 "2023-07-07T08:47:38Z")

</div>

that’s a colorpicker yes. a real bad one for an ide  
but sure it is one  
sorry for that: OjoH has one but a bad one I have to correct myself

---

<div class="post-metadata">

**Author:** ![Jeannot](https://ifnotnil.com/user_avatar/ifnotnil.com/jeannot/32/3787_2.png) [@Jeannot](https://ifnotnil.com/u/Jeannot)\
**Post date:** [7 July 2023 09:00 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/7 "2023-07-07T09:00:28Z")

</div>

no prob, I wasn’t sure either (Xojo is already far away in my brain). “Bad one” is debatable (from their perspective), as it is the one built-in into the OS (and those are usually bad, even on macOS).

The way they implemented it, is definitely nuts. Plus I just noticed that the loading time is quite slow.

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [7 July 2023 09:05 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/8 "2023-07-07T09:05:21Z")

</div>

so you want me to sell: they use the system one. Okay, they have none by self.  
That’s the conclusion. So Swing and FX are in a better shape. Both have their own

---

<div class="post-metadata">

**Author:** ![Jeannot](https://ifnotnil.com/user_avatar/ifnotnil.com/jeannot/32/3787_2.png) [@Jeannot](https://ifnotnil.com/u/Jeannot)\
**Post date:** [7 July 2023 09:45 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/9 "2023-07-07T09:45:30Z")

</div>

they are “native” 😉 (disclaimer: I don’t sell the idea of “native”, especially not for ColorPickers) 🙂

---

<div class="post-metadata">

**Author:** ![HalGumbert](https://ifnotnil.com/user_avatar/ifnotnil.com/halgumbert/32/2198_2.png) [@HalGumbert](https://ifnotnil.com/u/HalGumbert)\
**Post date:** [7 July 2023 13:39 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/10 "2023-07-07T13:39:08Z")

</div>

Maybe don’t draw it? Instead insert an already rendered image???

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [7 July 2023 14:32 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/11 "2023-07-07T14:32:49Z")

</div>

To be VERY clear the IDE is NOT Xojo  
It does have “Insert Color” that uses the system color picker  
Its a different one from a different green company

> [@Jeannot](#):
>
> A bit freaky that you have to use again events to get the value back,

yeah its the way the OS one works on macOS ☹  
Apple moved from a synchronous one to one that sends a notification asynchronously

> [@HalGumbert](#):
>
> Maybe don’t draw it? Instead insert an already rendered image???

Live sliders for the value and alpha make that not work ☹

I’ll see if other tools can make me a really fast one  
Maybe I’ll port this to a C# app

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [8 July 2023 03:01 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/12 "2023-07-08T03:01:32Z")

</div>

Do you want it circular or square?  
In recent versions of Xojo you can do singular direction gradients, which would simplify the drawing. Matching the value might be a bit difficult unless you manually create the gradient yourself.

For circular, you have the angle representing the hue, and the distance from the center representing the saturation.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [8 July 2023 03:12 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/13 "2023-07-08T03:12:23Z")

</div>

yeah the linear gradient brush might work if I wanted square

I might go that way JUST to make things quicker as drawing it the way I do now is a bit slow  
Its effectively pixel by pixel for the entire radius 😛

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [8 July 2023 03:16 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/14 "2023-07-08T03:16:03Z")

</div>

Its a shame the radial brush is for radius only. You could use some math to make your own angled gradient brush and combine that with a radial brush to simulate the saturation.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [8 July 2023 03:19 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/15 "2023-07-08T03:19:03Z")

</div>

Yeah a radial brush would need 360 degrees of colors to go from the centre out to the edges

I had thought maybe 360 degrees of lines drawn using a linear gradient  
at least that would mean only drawing 360 lines

hmmm … I suppose I could mess with that to see

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [8 July 2023 03:37 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/16 "2023-07-08T03:37:14Z")

</div>

It’s what I would try. Or you could create 1 path (representing a slice), rotate the context and fill the path 360 times 🙂

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [14 July 2023 17:22 UTC](https://ifnotnil.com/t/anyone-got-a-great-idea-on-how-to-draw-the-hsv-color-space-fast/3369/17 "2023-07-14T17:22:42Z")

</div>

OK well finally got my HSV to RGB color conversion right and it draws damned fast in C#

![Screen Shot 2023-07-14 at 11.21.10 AM](https://ifnotnil.com/uploads/default/original/2X/7/7003ae686816f9d6933b5e7b19d2d821152694c9.png)

Now to add the last few remaining bits ( a couple sliders & copying the selected color to the clipboard)

there’s a few pixel cracks and I’ll need to sort out how to fix those
