Drawing a colour wheel - bug in hcl?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Drawing a colour wheel - bug in hcl?

hadley wickham
Hi All,

I'm trying trying to draw a colour wheel (a slice of hcl space) in R.
Running the code below doesn't give me what I expect - there's some
oddly bright colours of the wrong hue around c(0, 0) and I see three
coloured circles (a small magenta, a medium sized yellow and a large
cyan).  Am I doing something wrong or is there a bug in the hcl code?

(Also any suggestions for generating a more evenly spaced grid of
colours would be greatly appreciated)

Regards,

Hadley

hcl <- expand.grid(h = seq(0, 360, by = 2), c = 70, l = seq(0, 100, by
= 2), fixup=FALSE)
hcl <- transform(hcl,
  angle = h * pi / 180,
  radius = l / 100
)
hcl <- transform(hcl,
  x = radius * sin(angle),
  y = radius * cos(angle)
)
hcl$colour <- hcl(hcl$h, hcl$c, hcl$l)

with(hcl, plot(x, y, col=colour, pch=20))

--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

Earl F. Glynn
"hadley wickham" <[hidden email]> wrote in message
news:[hidden email]...
>  there's some
> oddly bright colours of the wrong hue around c(0, 0) and I see three
> coloured circles (a small magenta, a medium sized yellow and a large
> cyan).  Am I doing something wrong or is there a bug in the hcl code?

You set c = 70 = constant

from ?hcl:

      c  The chroma of the color. The upper bound for chroma depends on hue
and luminance


You seem to have used the full range of possible hue and luminance values.
Perhaps your odd colored circles are violations on valid values of chroma?

R's HCL documentation (?hcl) isn't very helpful.  Foley and van Dam discuss
HLS, HSB, HSV, HVC color spaces in the "Computer Graphics" book, but not
HCL. The hcl function "corresponds to polar coordinates in the CIE-LUV color
space" but it's not clear how one converts from CIELUV to rgb from this
page: http://en.wikipedia.org/wiki/CIELUV_color_space.

BTW, the R documentation should be changed to eliminate "l" ("el") as
formal argument to the hcl function.

The book "Code Complete 2" suggests (p. 287) to "avoid names containing
hard-to-read characters".  I'd suggest "l" ("el") is perhaps the worst
possible variable name since it can so easily be confused with "1" ("one").

Also, using "hcl" in different ways in the same code is also discouraged by
"Code Complete 2".  Again on p. 287:  "Avoid the names of standard types,
variables, and routines."   If "hcl" is a "standard" routine, using "hcl" as
a variable name in the same code example using the "hcl" function is not
recommended and is confusing.

efg

Earl F Glynn
Bioinformatics
Stowers Institute for Medical Research

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

hadley wickham
On Mon, Jul 7, 2008 at 1:05 PM, Earl F. Glynn <[hidden email]> wrote:

> "hadley wickham" <[hidden email]> wrote in message
> news:[hidden email]...
>>  there's some
>> oddly bright colours of the wrong hue around c(0, 0) and I see three
>> coloured circles (a small magenta, a medium sized yellow and a large
>> cyan).  Am I doing something wrong or is there a bug in the hcl code?
>
> You set c = 70 = constant
>
> from ?hcl:
>
>      c  The chroma of the color. The upper bound for chroma depends on hue
> and luminance
>
>
> You seem to have used the full range of possible hue and luminance values.
> Perhaps your odd colored circles are violations on valid values of chroma?

Well, I do have fixup = FALSE, which the documentation indicates
should return NA if the chroma exceeds the actual upper bound.

> Also, using "hcl" in different ways in the same code is also discouraged by
> "Code Complete 2".  Again on p. 287:  "Avoid the names of standard types,
> variables, and routines."   If "hcl" is a "standard" routine, using "hcl" as
> a variable name in the same code example using the "hcl" function is not
> recommended and is confusing.

Yes, that's true!

Hadley


--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

Earl F. Glynn
"hadley wickham" <[hidden email]> wrote in message
news:[hidden email]...
> On Mon, Jul 7, 2008 at 1:05 PM, Earl F. Glynn <[hidden email]>
> wrote:
>> "hadley wickham" <[hidden email]> wrote in message
>> news:[hidden email]...

> Well, I do have fixup = FALSE, which the documentation indicates
> should return NA if the chroma exceeds the actual upper bound.

You did specify fixup in the hcl data.frame but I'm not sure how that info
was used when you called the hcl function since fixup is a parameter to the
hcl function.

Compare the two plots from below.  When you get the NAs you want, the odd
colors go away, but I'm not sure you'll like the results:

# I dropped fixup from the data.frame (and used variable names that "Code
Complete 2" might approve):

hcl.grid <- expand.grid(hue = seq(0, 360, by = 2),
                        chroma = 70,
                        luminance = seq(0, 100, by= 2))

hcl.grid <- transform(hcl.grid,
  angle = hue * pi / 180,
  radius = luminance / 100
)

hcl.grid <- transform(hcl.grid,
  x = radius * sin(angle),
  y = radius * cos(angle)
)

# What you have
hcl.grid$colour1 <- hcl(hcl.grid$hue, hcl.grid$chroma, hcl.grid$luminance,
                        fixup=TRUE)
with(hcl.grid, plot(x, y, col=colour1, pch=20))

# What you intended?
hcl.grid$colour2 <- hcl(hcl.grid$hue, hcl.grid$chroma, hcl.grid$luminance,
                        fixup=FALSE)
with(hcl.grid, plot(x, y, col=colour2, pch=20))


efg

Earl F Glynn
Bioinformatics
Stowers Institute for Medical Research

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

Achim Zeileis
In reply to this post by hadley wickham
Hadley:

> I'm trying trying to draw a colour wheel (a slice of hcl space) in R.
> Running the code below doesn't give me what I expect - there's some
> oddly bright colours of the wrong hue around c(0, 0) and I see three
> coloured circles (a small magenta, a medium sized yellow and a large
> cyan).  Am I doing something wrong or is there a bug in the hcl code?

All of these have been fixed and do not correspond to HCL colors.

> hcl <- expand.grid(h = seq(0, 360, by = 2), c = 70, l = seq(0, 100, by
> = 2), fixup=FALSE)
> hcl <- transform(hcl,
>  angle = h * pi / 180,
>  radius = l / 100
> )
> hcl <- transform(hcl,
>  x = radius * sin(angle),
>  y = radius * cos(angle)
> )
> hcl$colour <- hcl(hcl$h, hcl$c, hcl$l)

If you replace this by
   hcl$colour <- hcl(hcl$h, hcl$c, hcl$l, fixup = FALSE)

> with(hcl, plot(x, y, col=colour, pch=20))

Then, the resulting "wheel" has three overlapping "holes" corresponding to
the colors previously fixed.

Also, as hue/chroma correspond to polar coordinates in the UV plane of
CIELUV, it makes more sense to plot wheels in the hue/chroma plane for
fixed luminance. To draw a hue/luminance plane for fixed chroma is harder
to interpret geometrically.

hth,
Z

> --
> http://had.co.nz/
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

hadley wickham
In reply to this post by Earl F. Glynn
On Mon, Jul 7, 2008 at 4:30 PM, Earl F. Glynn <[hidden email]> wrote:

> "hadley wickham" <[hidden email]> wrote in message
> news:[hidden email]...
>> On Mon, Jul 7, 2008 at 1:05 PM, Earl F. Glynn <[hidden email]>
>> wrote:
>>> "hadley wickham" <[hidden email]> wrote in message
>>> news:[hidden email]...
>
>> Well, I do have fixup = FALSE, which the documentation indicates
>> should return NA if the chroma exceeds the actual upper bound.
>
> You did specify fixup in the hcl data.frame but I'm not sure how that info
> was used when you called the hcl function since fixup is a parameter to the
> hcl function.

Oh, oops, I got confused by exactly what you pointed out!

Hadley

--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

hadley wickham
In reply to this post by Achim Zeileis
> If you replace this by
>  hcl$colour <- hcl(hcl$h, hcl$c, hcl$l, fixup = FALSE)
>
>> with(hcl, plot(x, y, col=colour, pch=20))
>
> Then, the resulting "wheel" has three overlapping "holes" corresponding to
> the colors previously fixed.

Oops, thanks for pointing that out.

> Also, as hue/chroma correspond to polar coordinates in the UV plane of
> CIELUV, it makes more sense to plot wheels in the hue/chroma plane for fixed
> luminance. To draw a hue/luminance plane for fixed chroma is harder to
> interpret geometrically.

Oooh, I guess if I'd thought about that for a couple of seconds I
would have a realised that!  What are good values to use for luminance
and chroma in that case?  I can't seem to find a slice which gives me
a decently wheel-like slice?  Or should I just use fixup = TRUE, or
accept that true colour wheels displayed on rgb monitors don't look
like wheels?

Hadley

--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a colour wheel - bug in hcl?

Achim Zeileis
On Mon, 7 Jul 2008, hadley wickham wrote:

>> If you replace this by
>>  hcl$colour <- hcl(hcl$h, hcl$c, hcl$l, fixup = FALSE)
>>
>>> with(hcl, plot(x, y, col=colour, pch=20))
>>
>> Then, the resulting "wheel" has three overlapping "holes" corresponding to
>> the colors previously fixed.
>
> Oops, thanks for pointing that out.
>
>> Also, as hue/chroma correspond to polar coordinates in the UV plane of
>> CIELUV, it makes more sense to plot wheels in the hue/chroma plane for fixed
>> luminance. To draw a hue/luminance plane for fixed chroma is harder to
>> interpret geometrically.
>
> Oooh, I guess if I'd thought about that for a couple of seconds I
> would have a realised that!  What are good values to use for luminance
> and chroma in that case?

There are some utility visualization functions in the devel-version of
"colorspace" on R-Forge. Try:

source(url("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/paper/Colorspace/visualizations.R?rev=13&root=colorspace"))
hue_chroma_plane()
chroma_luminance_plane()

> I can't seem to find a slice which gives me
> a decently wheel-like slice?  Or should I just use fixup = TRUE, or
> accept that true colour wheels displayed on rgb monitors don't look
> like wheels?

They always look a bit awkward, more like rectangles or triangles, but you
could limit the maximum chroma, then it looks a bit more like a wheel.

hue_chroma_plane(c = 0:50)

But you can never get the full wheel if you want to balance with respect
to luminance. Or conversely, if you construct a wheel like

hue_saturation_plane()

then it is not balanced with respect to lumincance.
Z

> Hadley
>
> --
> http://had.co.nz/
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.