问题
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;
回答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
来源:https://stackoverflow.com/questions/58222353/the-formulars-in-hsv-triangle