Wednesday, October 31, 2012

Convertting a rgb color value to its hex-string

In a previous post, I have talked about how to convert a hex-string color to its rgbvalue. This time I will talk about the inversion, i.e., converting a rgb value color specification to its hex-string correspondence.

#pick the red color value out
red(rgbvalue)=rgbvalue/65536
#pick the green color value out
green(rgbvalue)=(rgbvalue%65536)/256
#pick the blue color value out
blue(rgbvalue)=rgbvalue%256
#convert dec color value to its hex string
dec2hex(dec)=gprintf("%02x",dec)
#convert rgb color value to its hex string
rgb2hex(color)="#".\
               dec2hex(red(color)).\
               dec2hex(green(color)).\
               dec2hex(blue(color))

color = 65535 #rgb-value 
print "The hex string of rgb-color ". color ." is: "
print rgb2hex(color)
plot cos(x) w line linecolor rgb rgb2hex(color)

Run this script, it will output the following string and picture.

The hex string of rgb color 65535 is: 
#00ffff

Fig.1 Converting from a rgb color specification to its hex-string


7 comments:

  1. Hadoop concepts, Applying modelling through R programming using Machine learning algorithms and illustrate impeccable Data Visualization by leveraging on 'R' capabilities.With companies across industries striving to bring their research and analysis (R&A) departments up to speed, the demand for qualified data scientists is rising.
    data science training in bangalore

    ReplyDelete
  2. Nice article. It's very helpful to me. Thank you for share with us. Can you please check my hexadecimal color codes online tool.

    ReplyDelete
  3. Wow! this is Amazing! Do you know your hidden name meaning ? Click here to find your hidden name meaning

    ReplyDelete
  4. Wow! this is Amazing! Do you know your hidden name meaning ? Click here to find your hidden name meaning

    ReplyDelete
  5. I have found that this site is very informative, interesting and very well written. keep up the nice high quality writing. Dried Whole Shrooms

    ReplyDelete

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.