[R] hex format

David Forrest drf5n at maplepark.com
Thu Apr 7 23:19:33 CEST 2005


On Thu, 7 Apr 2005, Earl F. Glynn wrote:

...
> picture, don't you "see" numbers?
>
> Maybe you don't see a number here, but I do. #ff0080 is interpreted in some
> (non-R) contexts as a single number.  In many contexts, including HTML,


> colors are represented as three bytes in hex with this notation and the "#"
> means "hexadecimal".  The RGB color componets can be discerned quite easily:
> hex FF is decimal 255 (red), hex 00 is decimal 0 (green), hex 80 is decimal
> 128 (blue).  Some programs, e.g., Dreamweaver, allow specification of colors
> in this hex 3-byte form directly.  The "16 million" colors you seen on a
> "true color" display are from the 256*256*256 (or in hex FF*FF*FF) possible
> RGB triples.
>
> > For example, R already provides both hsv() and rgb() to create colours
> > from vectors of three numbers, but the correspondence is different in each
> > case.
>
> Sorry if some consider this off topic:
> HSV as a color space is really only liked by computer scientists.  Image
> processing and color engineers rarely if ever use HSV.
>
> There are MANY other color spaces and computations possible (see "color
> spaces" or "color conversions" or other color topics on  this page
> http://www.efg2.com/Lab/Library/Color/Science.htm).  Most of these color
> manipulations in R are not easy because the very first step, converting
> colors, I mean numbers <g>, like #ff0080 to the red, green components is
> hindered because one must reinvent the wheel of hex-to-decimal conversion.

I think R has the hex to decimal OK, but might be lacking in the decimal
to hex case

zz<-function(x){
    x<-as.numeric(sub("#",'0x',x));
    c(x%/%256^2,
      x%/%256%%256,
      x%%256) }

> zz('#0f0e0d')
[1] 15 14 13
> zz('#ff0080')
[1] 255   0 128

If you already have the 3 byte triplet in read in as a binary, the same
integer arithmetic does the extraction.

> Perhaps R will someday introduce a "pixel" type that would encapsulate the
> three color components (for color images at least).  A matrix of pixels
> could easily be made into an image.  Some color computations such a Maxwell
> Triangle, or a CIE Chromaticity Chart (sorry the links are currently broken,
> but the image can be seen on this Chinese translation page)
> http://bluemoon.myrice.com/efg/color/chromaticity.htm in R is more difficult
> than it should be because of how R is designed now.  Many image processing
> statistical problems could be tackled directly in R if there were an easier
> way to manipulate pixels and images.
>
> But the hex manipulations I'm advocating could be used for variety of other
> purposes.  E.g, I must periodically deal with a binary data stream of flow
> cytometery data -- part ASCII, part binary.  Reading this stream directly
> from R would be nice and is almost doable.  Working with raw data and
> understanding  exactly what you've got would be facilitated by better
> conversion capabilities within R.

I'm still not sure what you mean by hex manipulations.

R has string manipulations, hex-to-number manipulations,
binary-file-to-number manipulations, mixed file to number manipulations,
and number to number manipulations.

What I think you are asking for is /displaying/ numbers.

Since R's sprintf() doesn't support the %x, (or %o, or %u) formats, I'm
not sure how to use R to translate the number 257 into #000101

zzinv<-function(x){????}
# such that:

> zzinv(257) #or zzinv(c(0,1,1))
"#000101"

Is zzinv() the operation you need?

Dave
-- 
 Dr. David Forrest
 drf at vims.edu                                    (804)684-7900w
 drf5n at maplepark.com                             (804)642-0662h
                                   http://maplepark.com/~drf5n/




More information about the R-help mailing list