2

I do not know how the following two formulas are derived. Please kindly explain. My reputation point is so low that I can't ask the person who wrote the formula.

HSV triangle in C#

var sat = (1 - 2*y1) / (sqrt3*x1 - y1 + 2);
var val = (sqrt3*x1 - y1 + 2) / 3;
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
san
  • 21
  • 3

1 Answers1

1

After the transformation, I needed the corners of the triangle to touch the unit circle at the points <0,-1> "top", <-√3/2,1/2> "bottom-left" and <√3/2,1/2> "bottom-right". I picked bottom-left to be the black point (val = 0), and when val = 1, the top would be the color point (val = 1 and sat = 1), and the bottom-right would be the white point (val = 1 and sat = 0).

To achieve this, I first defined the right edge as the equation

<x,y> = <0,-1>*sat + <√3/2,1/2>*(1-sat)
        ^            ^-- When sat = 0, the result is this point
        '-- When sat = 1, the result is this point

I then scaled this line towards <-√3/2,1/2> when val goes to zero

<x,y> = ( <0,-1>*sat + <√3/2,1/2>*(1-sat) )*val + <-√3/2,1/2>*(1-val)
<x,y> = <0,-1>*sat*val + <√3/2,1/2>*(1-sat)*val + <-√3/2,1/2>*(1-val)
        ^                ^                        ^-- When val = 0, the result is this point
        |                '-- When val = 1 and sat = 0, the result is this point
        '-- When val = 1 and sat = 1, the result is this point

Converting from vector form:

x = 0*sat*val + √3/2*(1-sat)*val + -√3/2*(1-val)
y = -1*sat*val + 1/2*(1-sat)*val + 1/2*(1-val)

Expanding:

x = -√3/2*val*sat + √3*val - √3/2
y = -3/2*val*sat + 1/2

Rearranging:

(√3*x - y + 2)/3 = val
(1 - 2*y)/3 = sat*val

Solving for sat and val:

sat = (1 - 2*y)/(√3*x - y + 2)
val = (√3*x - y + 2)/3
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • 1
    Thank you guys all! This will help me a lot. I've been trying to understand this formulas for a month. It's still not easy though :-), but I'll try to finish it. – san Oct 05 '19 at 04:05
  • Finally I understand those formulars. Your explanation was clear and enough. My mathematical knowledge isn't enough, so I was able to study it again, thanks to your formulars. What about make a mathematics lesson for software programmers? :-) Thanks again! – san Oct 09 '19 at 18:37