Color conversion.

There are many models to convert RGB to HSB.  I downloaded a dozen or so of them off the internet and coded them in until I found the one that mathematica uses.  The algorithm used here is modified from the one found on http://www.xbeat.net/vbspeed/c_HSLToRGB.htm.  HSLToRGB02 and RGBToHSL02.

Author:  Sean Ross
04 November 2003

Version 2.

Fixes a bug which returned negative RGB values, 17May2004

BeginPackage["Sean`RGBHSBconversion`"]

RGBtoHSB::usage="RGBtoHSB[red,green,blue] or RGBtoHSB[RGBColor[red,green,blue]] returns Hue[hue,saturation, brilliance] representing the same shade and color as the original RGBcolor.  By mathematica convention, red, green, blue, hue, saturation and brilliance are all numbers from 0 to 1.  Written by Sean Ross, May 2004 November 2003";
HSBtoRGB::usage="HSBtoRGB[hue,saturation,brilliance] or HSBtoRGB[Hue[hue,saturation,brilliance]] returns RGBColor[red,green, blue] representing the same shade and color as the original Hue.  By mathematica convention, red, green, blue, hue, saturation and brilliance are all numbers from 0 to 1.  Revision 2. Written by Sean Ross, May 2004";


Begin["`Private`"]

RGB to HSB conversion

Clear[RGBtoHSB];
RGBtoHSB[RGBColor[red_,green_,blue_]]:=RGBtoHSB[red,green,blue];
RGBtoHSB[{red_,green_,blue_}]:=RGBtoHSB[red,green,blue];

RGBtoHSB[red_,green_,blue_]:=

Block[{r,g,b,h,s,l,maxRGB,minRGB},
{r,g,b}=N[Abs[{red,green,blue}]];

If[(maxRGB=Max[r,g,b])>1,{r,g,b}={r,g,b}/maxRGB];

maxRGB=Max[r,g,b];
minRGB=Min[r,g,b];

l=maxRGB;

If[maxRGB==minRGB, Return[Hue[0,0,l]]];


s=(maxRGB-minRGB)/maxRGB;

contrast=maxRGB-minRGB;
Which[
	r==maxRGB,
		h=Mod[(g-b)/contrast,6],
	g==maxRGB,
		h=2.0+(b-r)/contrast,
	b==maxRGB,
		h=4.0+(r-g)/contrast

];
h=Abs[h/6];


Return[Hue[h,s,l]]
];

HSB to RGB conversion

Clear[HSBtoRGB];
HSBtoRGB[Hue[hue_,saturation_,brilliance_]]:=HSBtoRGB[hue,saturation,brilliance];
HSBtoRGB[Hue[hue_]]:=HSBtoRGB[hue,1,1];
HSBtoRGB[{hue_saturation_,brilliance_}]:=HSBtoRGB[hue,saturation,brilliance];

HSBtoRGB[hue_,saturation_,brilliance_]:=

Block[{r,g,b,h,s,l,t1,t2,t3,max,min,mid},

{h,s,l}=Abs[Map[(If[#==1,#,Mod[#,1]])&,{hue,saturation,brilliance}]];


If[s==0,Return[RGBColor[l,l,l]]];
If[l==0,Return[RGBColor[0,0,0]]];

max=l;
min=(1-s) l;
q=(max-min)*6;

Which[
h<(1/6),
	{r,g,b}={max,mid=h q + min,min},
h<(1/3),
	{r,g,b}={mid=(h-1/3) q + min,max,min},
h<(1/2),
	{r,g,b}={min,max,mid=(h-1/3)q + min},
h<(2/3),
	{r,g,b}={min,mid=(2/3-h)q + min,max},
h<(5/6),
	{r,g,b}={mid= (h-2/3) q + min,min,max},
True,
	{r,g,b}={max,min,(1-h) q + min}						

];



Return[RGBColor[Abs[r],Abs[g],Abs[b]]]
];

End[];
EndPackage[];

Testing routines and color swatches

{0.0387474,  0.916162,  0.0398472}

r=0.0387474;g=0.916162;b= 0.0398472;

RGBtoHSB[RGBColor[r,g,b]]

Show[GraphicsArray[{
Graphics[
{RGBColor[r,g,b],Rectangle[{0,0},{1,1}]}]
,
Graphics[
{RGBtoHSB[RGBColor[r,g,b]],Rectangle[{0,0},{1,1}]}]
,
Graphics[
{Hue[.85,1.,1.],Rectangle[{0,0},{1,1}]}]
}]
]

h=0.2; s=1.;b=1.;

Show[GraphicsArray[{
Graphics[
{Hue[h,s,b],Rectangle[{0,0},{1,1}]}]
,
Graphics[
{HSBtoRGB[Hue[h,s,b]],Rectangle[{0,0},{1,1}]}]
,
Graphics[
{RGBColor[.8,1.,0.],Rectangle[{0,0},{1,1}]}]
}]
]