MathGroup Archive 2001

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

Search the Archive

Re: RGBColor to Hue?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31059] Re: RGBColor to Hue?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 6 Oct 2001 03:32:31 -0400 (EDT)
  • References: <9pjgk0$3fu$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ted,
There is a problem with your code when the least two of  red, grn, blu are
equal : we get division by zero.
Below, I use a Mathematica version of the code outlined at the URL
         http://www.efg2.com/Lab/Graphics/Colors/HSV.htm
provided by Bart van der Wolf :

    Unprotect[ToColor];

    ToColor[RGBColor[r_,g_,b_], Hue]:=
        Module[{v,del,s,h},
             v = Max[r,g,b];
             del= v- Min[r,g,b];
             s= If[v==0,0, del/v];
             h=
             Which[
                  s==0, 0,
                  r==v, (g-b)/(6 del),
                  g==v, 1/3+ (b-r)/(6 del),
                  b==v, 2/3+ (r-g)/(6 del)
               ];
            If[h<0, h=1+h];
            Hue[h,s,v]
         ];

    ToColor[CMYKColor[r_,g_,b_], Hue]:=
         ToColor[ToColor[CMYKColor[r,g,b],RGBColor], Hue];
    ToColor[GrayLevel[v_], Hue]:=
         ToColor[ToColor[GrayLevel[v],RGBColor], Hue];

    Protect[ToColor];

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Ersek, Ted R" <ErsekTR at navair.navy.mil> wrote in message
news:9pjgk0$3fu$1 at smc.vnet.net...
> 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: Possible Incorrect Summation
  • Next by Date: Re: Parallel Processing Toolkit
  • Previous by thread: Re: RGBColor to Hue?
  • Next by thread: RE: Legend in ParametricPlot