MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RGBColor to Hue?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31039] Re: RGBColor to Hue?
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Fri, 5 Oct 2001 01:22:53 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dave Park wanted a transformation that will convert an RGBColor
specification to Hue[h,s,b]. 

I spent some time working on this a few years ago, and found that the
definitions below perform a perfect transformation (as far as I can tell).
This is an extension of the undocumented function ToColor[ ]. 

(***********************)
Unprotect[ToColor];

ToColor[RGBColor[red_,grn_,blu_],Hue]/;red>=grn>=blu:=
  Hue[(grn-blu)/(red-blu)/6, (red-blu)/red, red ]
 
ToColor[RGBColor[red_,grn_,blu_],Hue]/;grn>=red>=blu:=
  Hue[1/3-(red-blu)/(grn-blu)/6, (grn-blu)/grn, grn ]
 
ToColor[RGBColor[red_,grn_,blu_],Hue]/;grn>=blu>=red:=
  Hue[1/3+(blu-red)/(grn-red)/6, (grn-red)/grn, grn ]
 
ToColor[RGBColor[red_,grn_,blu_],Hue]/;blu>=grn>=red:=
  Hue[2/3-(grn-red)/(blu-red)/6, (blu-red)/blu,blu ]
 
ToColor[RGBColor[red_,grn_,blu_],Hue]/;blu>=red>=grn:=
  Hue[2/3+(red-grn)/(blu-grn)/6, (blu-grn)/blu,blu ]
 
ToColor[RGBColor[red_,grn_,blu_],Hue]/;red>=blu>=grn:=
  Hue[1-(blu-grn)/(red-grn)/6, (red-grn)/red,red ]

(*********************)

Example:
In[8]:=
   ToColor[RGBColor[0.76, 1., 0.7], Hue]

Out[8]=
   Hue[0.3, 0.3, 1.]


My definitions compute the (RGBColor --> Hue) transformation above.  
The built-in ToColor[ ] definition performs the inverse operation below and
gives some indication that my transformation is correct.  I think you can
use either to specify the color of graphics and get the same result.

In[9]:=
   ToColor[Hue[0.3, 0.3, 1], RGBColor]

Out[9]=
   RGBColor[0.76, 1., 0.7]

This enhancement to the undocumented ToColor[ ] function is one of the handy
enhancements provided in my ColorPlot package posted at 
http://www.mathsource.com/cgi-bin/msitem?0210-700

---
Regards,
 Ted



  • Prev by Date: RE: RGBColor to Hue?
  • Next by Date: Using NDSolve for 2-variables functions ?
  • Previous by thread: RE: RGBColor to Hue?
  • Next by thread: Re: RGBColor to Hue?