Octave, 65 60 50 bytes
Edit: Saved 10 bytes thanks to pawel.boczarski
An approximate solution...
@(c)mod(atan2d(.866*c*[0;1;-1],c*[2;-1;-1]/2),360)
Test run
@(c)mod(atan2d(.866*c*[0;1;-1],c*[2;-1;-1]/2),360)
ans([0 182 255])
ans = 196.14
@(c)mod(atan2d(.866*c*[0;1;-1],c*[2;-1;-1]/2),360)
ans([127 247 103])
ans = 111.05
@(c)mod(atan2d(.866*c*[0;1;-1],c*[2;-1;-1]/2),360)
ans([0 0 1])
ans = 240.00
@(c)mod(atan2d(.866*c*[0;1;-1],c*[2;-1;-1]/2),360)
ans([255 165 245])
ans = 305.82
Octave, 107 bytes
My original (exact-ish) solution...
###Code: function H=r(c) [b,i]=sort(c);h=60*6*(i(1)~=3),2,4;H=(h(3)-h(2))*(1+(b(3)-b(2))/(b(3)-b(1)))/2+h(2);
Code:
###Explained:
function H=r(c) [b,i]=sort(c);h=60*[6*(i(1)~=3),2,4](i);H=(h(3)-h(2))*(1+(b(3)-b(2))/(b(3)-b(1)))/2+h(2);
Explained:
function H=r(c)
[b,i]=sort(c);
h=60*[6*(i(1)~=3),2,4](i);
H=(h(3)-h(2))*(1+(b(3)-b(2))/(b(3)-b(1)))/2+h(2);
This function takes a vector containing the R,G,B values as input c
and sorts the input in ascending order
b
contains the sorted values [z, y, x]i
contains the RGB plane associated with each value in b
The vector h
is populated with the values
60*[6, 2, 4]
=[360, 120, 240]
(but 3 bytes shorter)- unless the lowest value is in Blue (
i(1) == 3
), in which case the first hue value becomes zero - then use
(i)
to rearrangeh
into[h(Z), h(Y), h(X)]
order
From there it's just a straight transcription of the formula. You can try it here.