[Question]
  [
You are given the RGB values of a color. Your task is simple: to calculate the hue, in the simplest definition.
Say the channels with highest, middle and lowest value are X, Y, Z (which are either red, green or blue) and their values are x, y, z. The hue of this color is (h(X)-h(Y))\*(1 + (x-y)/(x-z))/2 + h(Y), where:
```
h(red) = 0 (or 360 if one of X or Y is blue)
h(green) = 120
h(blue) = 240
```
The input consists of 3 integers from 0 to 255 which are not all equal, in any consistent order. The output can be floats, or integers rounded either up or down, which doesn't have to be consistent. If the integer part of the output is 0 or 360, you can print either of them.
You cannot call builtins for color space conversions, including implicit conversions such as while manipulating an image.
This is code-golf. Shortest code wins.
## Examples
```
Input:  0 182 255
Output: 197 (or 198)
Input:  127 247 103
Output: 110
Input:  0 0 1
Output: 240
Input:  255 165 245
Output: 307 (or 306)
```
## Edit
You don't have to follow the exact formula, but only have to give the same result as the above formula. I'd like to also see some answers golfing the formula itself.
  
[Answer]
# C#, ~~188~~ ~~210~~ ~~206~~ ~~197~~ 191 bytes
```
int H(int r,int g,int b){int[]a={r,g,b};System.Array.Sort(a);int x=a[2],y=a[1],c=x==g?1:(x==b?2:(y==b?3:0)),d=y==g?1:(y==b?2:(x==b?3:0));return(int)((c-d)*120*(1+(x-y)*1D/(x-a[0]))/2+d*120);}
```
Thanks to Sok for saving 4 bytes and to SLuck49 for saving 15!
[Answer]
## Pyth, ~~41~~ ~~55~~ ~~53~~ 51 bytes
```
A.)JohN,VQ*L120?qeQhSQ3j312T+/*-HKeeJhc-GheJ-GhhJ2K
```
Input is expected in the form `r,g,b`. Here's an explanation:
```
                                                        Implicit: Q=eval(input()), evaluates to (r,g,b)
               ?qeQhSQ                                  Is b the smallest?
                      3j312T                            Choose [0,1,2] or [3,1,2] based on above
          *L120                                         Convert to [0,120,240] or [360,120,240]
       ,VQ                                              Pair -> [[r,0/360],[g,120],[b,240]]
   JohN                                                 Order by 1st element in each pair, store in J
A.)J                                                    Pop biggest from J, set G = x, H = h(X)
                                                        Output calculation:
                                       -GheJ                x - y
                                            -GhhJ           x - z
                                     hc                     Divide and increment
                                 KeeJ                       Set K = h(Y)
                              *-HK                          Multiply by (h(X) - h(Y))
                             /                   2          Integer division by 2
                            +                     K         Add h(Y)
```
*Saved 4 bytes, thanks to @Jakube and @isaacg*
[Answer]
# Javascript (ES6), ~~145~~ ~~115~~ ~~108~~ ~~100~~ ~~97~~ 90 bytes
Returns floats. Assign to a function to use.
```
(r,g,b)=>([x,y,z]=[r,g,b].sort((a,b)=>b-a),m=x-z,(x-r?x-g?r-g+4*m:b-r+2*m:g-b+6*m)/m%6*60)
```
Saved 30 bytes by inlining everything into a single ternary operator sequence and waiting until the end to normalize to 0-360.
Thanks to edc65, Vasu Adari, and ETHproductions for saving even more bytes.
[JSFiddle](http://jsfiddle.net/ndbkx9jk/) with tests. Try in Firefox.
If removing the function declaration `h=` is not legal, add 2 bytes.
[Answer]
# Pyth, 27 bytes
```
*60%+c-Ft.<QJxQKeSQ-KhSQyJ6
```
[Demonstration.](https://pyth.herokuapp.com/?code=*60%25%2Bc-Ft.%3CQJxQKeSQ-KhSQyJ6&input=255%2C+165%2C+245&test_suite_input=0%2C+182%2C+255%0A127%2C+247%2C+103%0A0%2C+0%2C+1%0A255%2C+165%2C+245%0A1%2C+0%2C+0%0A0%2C+1%2C+0&debug=0)
[Test harness.](https://pyth.herokuapp.com/?code=*60%25%2Bc-Ft.%3CQJxQKeSQ-KhSQyJ6&input=255%2C+165%2C+245&test_suite=1&test_suite_input=0%2C+182%2C+255%0A127%2C+247%2C+103%0A0%2C+0%2C+1%0A255%2C+165%2C+245%0A1%2C+0%2C+0%0A0%2C+1%2C+0&debug=0) 
Fomula taken from [Wikipedia](https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma).
Essentially, the steps are:
1. `.<QJxQKeSQ`: Roate the largest value to the front of the list.
2. `-Ft`: Take the difference of the other two values.
3. `-KhSQ`: Subtract the minimum value from the maximum value.
4. `c`: Divide 2 by 3.
5. `+ ... yJ` Add twice the index of the maximum value in the list (0 if R, 2 if G, 4 if B).
6. `% ... 6`: Mod 6, to fix issues with negatives.
7. `*60`: Multiply by 60 to convert to degrees, and print.
[Answer]
# 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](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 rearrange `h` into `[h(Z), h(Y), h(X)]` order
From there it's just a straight transcription of the formula.
You can try it [here](http://ideone.com/vkJLYJ).
[Answer]
# Pyth, 55
I know @Sok's answer beats mine, but since I finished mine just after he/she posted, I thought I would still post. This was my first time using Pyth so I'm sure I made some obvious mistakes.
```
DlZK*120ZRKJSQFNJ=Y+YxQN)=kl@Y1+k/*-leYk+1c-eJ@J1-eJhJ2
```
Input is expected to be `r, g, b`. You can try it [here](https://pyth.herokuapp.com/?code=DlZK*120ZRKJSQFNJ%3DY%2BYxQN)%3Dkl%40Y1%2Bk%2F*-leYk%2B1c-eJ%40J1-eJhJ2&debug=0).
[Answer]
## PowerShell, ~~232~~ ~~226~~ ~~222~~ 161 Bytes
See revision history for previous versions
```
$z,$y,$x=($r,$g,$b=$args)|sort
$c=((2,(0,3)[$y-eq$b])[$x-ne$b],1)[$x-eq$g]
$d=((2,(0,3)[$x-eq$b])[$y-ne$b],1)[$y-eq$g]
(($c-$d)*120*(1+($x-$y)/($x-$z))/2+$d*120)
```
Hoo boy, let's see if I can walk through this. Since `\n` counts the same as `;` I left the line breaks in for clarity.
The first line takes input as three `$args` and stores them into `$r, $g, $b`. We're really only going to be using `$b` later, but we need all three so the `|sort` works appropriately. This makes `$z, $y, $x` the smallest-to-largest of the input arguments.
The next two lines setup `$c` and `$d` by using multiple index-into-an-array calls to set the numbers appropriately. Working from outside in, if `$x` is `-eq`ual to `$g` (i.e., green was the largest), we set `$c=1` ... else, if `$x` is `-n`ot`e`qual to `$b` (i.e., blue wasn't the largest) `$c` is either `0` or `3` depending if blue was the second largest ... else, `$c=2`. Similar logic sets `$d`.
We then calculate and print the output with the following, which is just the algorithm from the challenge golfed a little bit.
```
(($c-$d)*120*(1+($x-$y)/($x-$z))/2+$d*120)
```
[Answer]
# Ruby, ~~117~~ ~~96~~ 94 bytes
**Code:**
```
h=->r,g,b{z,y,x=[r,g,b].sort;v=x-z.to_f;({r=>(g-b)/v,g=>2+(b-r)/v,b=>4+(r-g)/v}[x]%6*60).to_i}
```
* Saved 21 bytes by removing `()` and using r,g,b variables.
* Taking modulus of 6 to convert negative value and multiplying it
by 60 to convert to degrees that saved 2 bytes.
**Examples:**
```
irb(main):274:0> h.call 0,182,255
=> 197
irb(main):275:0> h.call 127,247,103
=> 110
irb(main):276:0> h.call 0,0,1
=> 240
irb(main):277:0> h.call 255,165,245
=> 306
```
[Answer]
# SWI-Prolog, 133 bytes
```
a(L,H):-L=[R,G,B],max_list(L,X),min_list(L,Y),member(X:I:J:U,[R:G:B:0,G:B:R:2,B:R:G:4]),Z is 60*(U+(I-J)/(X-Y)),(Z<0,H is Z+360;H=Z).
```
Example: `a([255,165,245],Hue).` outputs `Hue = 306.666666666666 .`
This uses the following formula:
* `Max = max(R,G,B)`, `Min = min(R,G,B)`.
* If `Max = R`, `U = 0`. Else if `Max = G`, `U = 2`. Else `U = 4`.
* If `Max = R`, `I = G` and `J = B`. Else if `Max = G`, `I = B` and `J = R`. Else `I = R` and `J = G`.
* `Z = U + (I - J)/(Max - Min)`
* `Hue` is either `Z` or `Z + 360` if `Z < 0`.
[Answer]
# Perl 5, 138 132 119 bytes
### Code:
```
($m,$c,$M)=sort@A=($R,$G,$B)=@ARGV;print 60*(6+$M>$m?($G>$c?$B-$R:$B>$c?$R-$G:$G-$B)/($M-$m)+($G>$c?2:$B>$c?4:0):0)%360
```
### Remarks:
Surely Perl can't win such challenge with all the Pyth'oresque golfing. 
But I wondered if this was possible to do with only 1 calculation step.
Thanks to the modulus that worked out nicely. :)
### Test:
```
$ perl hue.pl 0 182 255
197
$ perl hue.pl 127 247 103
110
$ perl hue.pl 0 0 1
240
$ perl hue.pl 255 165 245
307
```
[Answer]
# R, 125 bytes
Very similar to beaker's Octave solution. Floating point output.
## Code:
```
h=function(x){
  o=seq(3)[order(-x)];
  y=c(60*c(6*(o[3]!=3),2,4)[o],x[o]);
  return((y[1]-y[2])*(1+(y[4]-y[5])/(y[4]-y[6]))/2+y[2]);
}
```
## Examples:
```
> h(c(0,182,255))
[1] 197.1765
> h(c(127,247,103))
[1] 110
> h(c(0,0,1))
[1] 240
> h(c(255,165,245))
[1] 306.6667
```
[Answer]
# Python, 154 bytes
```
def h(c):r=c[:];c.sort();c=c[::-1];x,y,z=c;i,j=[120if n==r[1]else 240if n==r[2]else 0if z==r[2]else 360for n in[x,y]];print ((i-j)*(1+(x-y+0.)/(x-z))/2)+j
```
Accepts a list of values. Not sure if this can be broken down further. Here it is ungolfed:
```
def hue(color):
 rgb=color[:]  # copy list
 color.sort()  # sort list
 color=color[::-1]  # reverse sort
 x,y,z=color   # pull out x,y,z
 # The line 
 #   i,j=[120if n==r[1]else 240if n==r[2]else 0if z==r[2]else 360for n in[x,y]]
 # is basically the following, twice, once for x/hx and the second time for y/hy
 if x==rgb[1]: # if x is green
  hx = 120
 else:
  if x==rgb[2]: # if x is blue
   hx = 240
  else:
   if z==rgb[2]: # if z is blue and x is red
    hx = 0
   else:       # if x is red and y is blue
    hx = 1
 print ((hx-hy)*(1+(x-y+0.)/(x-z))/2)+hy  # calculate, print
```
[Answer]
# JavaScript 108
Alternate method.
```
function H(r,g,b){a=[r,g,b].sort(),M=a[2],c=M-a[0],h=M==r?(g-b)/c%6:M==g?(b-r)/c+2:(r-g)/c+4
return h*60|0;}
```
**JavaScript 194**
Using the example method. 
```
Array.prototype.i=[].indexOf
function H(r,g,b,a){a=[r,g,b].sort(),i=[a.i(r),a.i(g),a.i(b)],x=[i[2]?360:0,120,240],hx=x[i.i(2)]|0,hy=x[i.i(1)]|0
return (hx-hy)*(1+(a[2]-a[1])/(a[2]-a[0]))/2+hy|0}
var input = document.getElementById("input").innerHTML;
var output = document.getElementById("output");
var html = "";
input.replace(/(\d+)\,(\d+)\,(\d+)/g, function(m, r, g, b) {
  html += H(r, g, b) + "\n";
});
output.innerHTML = html;
```
```
<pre id="input">
0,182,255
127,247,103
0,0,1
255,165,245
</pre>
<pre id="output">
</pre>
```
]