POV-Ray : Newsgroups : povray.text.scene-files : HEX --> RGB Server Time
27 Dec 2024 03:46:24 EST (-0500)
  HEX --> RGB (Message 1 to 5 of 5)  
From: Mark Donovan
Subject: HEX --> RGB
Date: 13 Oct 1998 21:16:35
Message: <3623ED22.72C5BC97@worldnet.att.net>
Okay, if you see the perfect color on a web page and all you have is the
HTML hex color string, you can use it in your POV-Ray scene with this
include file. There's a demo scene to display a bunch of hex color
samples at the end. It might also be handy if you want to create a HTML
color palette.

Rather than leave this rather slow macro in a scene, it's better to
replace the macro call with the RGB color in a final scene. Here's how
to get the RGB values.

#declare RGB = hex2rgb("a0beef");
#debug concat("<", str(RGB.x, 6, -1), ", ", str(RGB.y, 6, -1), ", ",
str(RGB.z, 6, -1), ">\n")

Copy the color from the debug output and replace the macro call in the
scene. 

Mark
===
#local Version_Temp = version;
#version 3.1;

#ifdef(View_POV_Include_Stack)
#   debug "including hex2rgb.inc\n"
#end

/*
   Convert HEX to RGB color space
   Date:   October 1998
   Author: Mark Donovan

 Description:

   Converts a hexadecimal string a POV-Ray color. Each component is
   defined in the range 00 to ff. The hexadecimal value must be six
   valid hexadecimal digits with leading zeros for red, green an blue
   components.

 Typical call:

   #include "hex2rgb.inc"
   // HEX is a string is a string of six hexadecimal digits
   #declare RGB = hex2rgb("a0beef");
   // returns POV-Ray color RGB = color rgb <red, green, blue>

*/

#macro hex2rgb(HEXVAL)

#local _HEX_V = strlwr(HEXVAL)
#if (strlen(_HEX_V) != 6)
  #error "Length must be exactly six hex digits"
#end
#local _HEX_i = 1;
#while (_HEX_i <= 6)
  #local _HEX_DIGIT = substr(_HEX_V, _HEX_i, 1)
  #switch (asc(_HEX_DIGIT))
  #range (asc("0"), asc("9"))
    #local _HEX_DEC = asc(_HEX_DIGIT) - asc("0");
  #break
  #range (asc("a"), asc("f"))
    #local _HEX_DEC = asc(_HEX_DIGIT) - asc("a") + 10;
  #break
  #else
    #error concat("Bad hex digit in ", HEXVAL)
  #end
  #if (mod(_HEX_i, 2) = 1)
    #local _HEX_COLOR = _HEX_DEC;
  #else
    #local _HEX_COLOR = _HEX_COLOR*16 + _HEX_DEC;
  #end
  #switch (_HEX_i)
  #case (2)
    #local _HEX_R = _HEX_COLOR / 255;
  #break
  #case (4)
    #local _HEX_G = _HEX_COLOR / 255;
  #break
  #case (6)
    #local _HEX_B = _HEX_COLOR / 255;
  #break
  #end
  #local _HEX_i = _HEX_i + 1;
#end
color rgb <_HEX_R, _HEX_G, _HEX_B>
#end

#version Version_Temp;
===
// Persistence of Vision Ray Tracer Scene Description File
// File: hsvcolor.pov
// Vers: 3.1
// Desc: Demo scene for HEX to RGB conversion.
// Date: September 2, 1998
// Auth: Mark Donovan
//

#version 3.1;

#include "colors.inc"
#include "hex2rgb.inc"

global_settings { assumed_gamma 1.0 }

#declare COLS = 20;
#declare ROWS = 16;
#declare STEPS = ROWS*COLS;
#declare R = 0.05;

camera {
  orthographic
  location  <0.0, 0.1, -4.0>
  up        2.5*y
  right     2.5*4/3*x
  look_at   <0.0, 0.0,  0.0>
}

#declare hexcolors = array [263] {
  "010203", "040506", "070809", "0a0b0c", "0d0e0f", "101112",
  "131415", "161718", "191a1b", "1c1d1e", "1f2021", "222324",
  "252627", "28292a", "2b2c2d", "2e2f30", "313233", "343536",
  "373839", "a5a5a5", "000000", "000080", "0000ff", "008000",
  "008080", "0080ff", "00ff00", "00ff80", "00ffff", "800000",
  "800080", "8000ff", "808000", "808080", "8080ff", "80ff00",
  "80ff80", "80ffff", "ff0000", "ff0080", "ff00ff", "ff8000",
  "ff8080", "ff80ff", "ffff00", "ffff80", "ffffff", "000000",
  "000033", "000066", "000099", "0000cc", "0000ff", "003300",
  "003333", "003366", "003399", "0033cc", "0033ff", "006600",
  "006633", "006666", "006699", "0066cc", "0066ff", "009900",
  "009933", "009966", "009999", "0099cc", "0099ff", "00cc00",
  "00cc33", "00cc66", "00cc99", "00cccc", "00ccff", "00ff00",
  "00ff33", "00ff66", "00ff99", "00ffcc", "00ffff", "330000",
  "330033", "330066", "330099", "3300cc", "3300ff", "333300",
  "333333", "333366", "333399", "3333cc", "3333ff", "336600",
  "336633", "336666", "336699", "3366cc", "3366ff", "339900",
  "339933", "339966", "339999", "3399cc", "3399ff", "33cc00",
  "33cc33", "33cc66", "33cc99", "33cccc", "33ccff", "33ff00",
  "33ff33", "33ff66", "33ff99", "33ffcc", "33ffff", "660000",
  "660033", "660066", "660099", "6600cc", "6600ff", "663300",
  "663333", "663366", "663399", "6633cc", "6633ff", "666600",
  "666633", "666666", "666699", "6666cc", "6666ff", "669900",
  "669933", "669966", "669999", "6699cc", "6699ff", "66cc00",
  "66cc33", "66cc66", "66cc99", "66cccc", "66ccff", "66ff00",
  "66ff33", "66ff66", "66ff99", "66ffcc", "66ffff", "990000",
  "990033", "990066", "990099", "9900cc", "9900ff", "993300",
  "993333", "993366", "993399", "9933cc", "9933ff", "996600",
  "996633", "996666", "996699", "9966cc", "9966ff", "999900",
  "999933", "999966", "999999", "9999cc", "9999ff", "99cc00",
  "99cc33", "99cc66", "99cc99", "99cccc", "99ccff", "99ff00",
  "99ff33", "99ff66", "99ff99", "99ffcc", "99ffff", "cc0000",
  "cc0033", "cc0066", "cc0099", "cc00cc", "cc00ff", "cc3300",
  "cc3333", "cc3366", "cc3399", "cc33cc", "cc33ff", "cc6600",
  "cc6633", "cc6666", "cc6699", "cc66cc", "cc66ff", "cc9900",
  "cc9933", "cc9966", "cc9999", "cc99cc", "cc99ff", "cccc00",
  "cccc33", "cccc66", "cccc99", "cccccc", "ccccff", "ccff00",
  "ccff33", "ccff66", "ccff99", "ccffcc", "ccffff", "ff0000",
  "ff0033", "ff0066", "ff0099", "ff00cc", "ff00ff", "ff3300",
  "ff3333", "ff3366", "ff3399", "ff33cc", "ff33ff", "ff6600",
  "ff6633", "ff6666", "ff6699", "ff66cc", "ff66ff", "ff9900",
  "ff9933", "ff9966", "ff9999", "ff99cc", "ff99ff", "ffcc00",
  "ffcc33", "ffcc66", "ffcc99", "ffcccc", "ffccff", "ffff00",
  "ffff33", "ffff66", "ffff99", "ffffcc", "ffffff"
}

#declare i = 0;
#declare r = 1;
#declare c = 1;
#while (i < dimension_size(hexcolors, 1))
  #declare RGB = hex2rgb(hexcolors[i]);
  sphere { <(c - COLS/2 - 0.5) * 3*R, (ROWS/2 - r + 0.5) * 3*R, 0>, R
    texture {
      pigment { color RGB }
      finish { ambient 1 }
    }
  }
  #declare c = c + 1;
  #if (c > COLS)
    #declare c = 1;
    #declare r = r + 1;
    #if (r > ROWS)
      #declare r = 1;
    #end
  #end
  #declare i = i + 1;
#end
===


Post a reply to this message

From: Josh English
Subject: Re: HEX --> RGB
Date: 16 Oct 1998 11:48:46
Message: <36276B37.B9EC73BD@spiritone.com>
Very cool! And very helpful. SDince we're on the subject, I am working
on some images for a friend that are set in a city. They're pretty
relaxed about how the building are going to look, and in my sample
scenes they didn't like the sky. I had just used some of the preset
cloudy skies. They want the sky color to be Hue 205, Saturation 120, and
Luminosity between 56 and 136. I don't have any way of  converting this
color! Has anyone seen this color palette? Does anyone know where I
could find resources too convert it?

Thanks in advance

Josh English
eng### [at] spiritonecom
www.spiritone.com/~english


Post a reply to this message

From: Ken
Subject: Re: HEX --> RGB
Date: 16 Oct 1998 12:09:16
Message: <3627617C.849E0C62@pacbell.net>
Josh English wrote:

> Very cool! And very helpful. SDince we're on the subject, I am working
> on some images for a friend that are set in a city. They're pretty
> relaxed about how the building are going to look, and in my sample
> scenes they didn't like the sky. I had just used some of the preset
> cloudy skies. They want the sky color to be Hue 205, Saturation 120, and
> Luminosity between 56 and 136. I don't have any way of  converting this
> color! Has anyone seen this color palette? Does anyone know where I
> could find resources too convert it?
>
> Thanks in advance
>
> Josh English
> eng### [at] spiritonecom
> www.spiritone.com/~english

  That's an easy one. With the numbers you gave, having averaged
you last entry to 75, I am getting an rgb value of rgb 205-120-75
or in pov lingo =
pigment{ color (colour) rgb< 0.8039, 0.4705, 0.0.2941 >}

   I cheated to come up with this for you and you can cheat to if you
have access to PSP, possibly the windows custom color picker
window, or just about any of the half way decent paint programs out
there today. I haven't checked but windows paint probably has it too.
  In PSP you can go into the color control panel and input the values
you gave. There are windows for hue, saturation, and luminance.
When your through inputting - numbers will appear on the right
corresponding to a scale from 0 - 255. It is then an easy operation
to convert that to Pov syntax- n/255 = pov rgb.

Ken Tyler


Post a reply to this message

From: Josh English
Subject: Re: HEX --> RGB
Date: 16 Oct 1998 12:23:33
Message: <36277361.B2B24E3@spiritone.com>
Thanks. Mark Donovans post explained it to me. My newsrteader wasn't
letting me see it for some reason until after I posted my message.
(grrrrr). I am on a Mac, and the only color palette conversion programs
I have are RGB3, and HTML Color Picker, and Photshop 3.05. I couldn't
find anything else fort he Mac.

Josh English
eng### [at] spiritonecom
www.spiritone.com/~english


Post a reply to this message

From: Mark Donovan
Subject: Re: HEX --> RGB
Date: 16 Oct 1998 23:34:21
Message: <36280225.C157F51E@worldnet.att.net>
Glad it's useful! I guess Josh found the HSL --> RGB conversion I posted
a few days ago. The first part of Josh's Hue, Saturation, Luminosity
problem is easy. My POV-Ray color space conversions use values between
zero and one, while color is often specified by values between 0 and
255. Just divide by 255 to fix that.

However, there's a big gotcha in all this. All color spaces are not
created equal! One problem is the hue defined at zero. The color space I
use defines red as the zero value of hue (that's the standard used by
Tektronix, HP and Apple color spaces). Yet other definitions set the
zero value to blue. It's easy to convert the zero value from blue to red.

  #declare Hue_Red_Zero = mod(Hue_Blue_Zero + 2/3, 1.0);

The term HSL can also be confusing. It can mean Hue, Saturation, and
Lightness, or it can mean Hue, Saturation, and Luminosity. My hsl2rgb
conversion is for Lightness. I included an HSB (Hue, Saturation, and
Brightness) conversion which is probably the same as Hue, Saturation,
and Luminosity. The difference is that Lightness is defined so that
black is zero, white is 1.0 and fully saturated color happens at
lightness value 0.5. Luminosity is usually defined so that black is zero
and fully saturated color happens at luminosity value 1.0.

Josh's HSL sample points out this problem with color space definitions.
Whenever you get a color value, it's important to know how the color
space is defined. Neglecting gamma correction, RGB is a well defined
standard. The various HSL, HSB, HSB, and HLS color spaces aren't so well
standardized. Josh's hue 205 converted by dividing by 255 produces a
shade of violet. I suspect the 205 hue is from color space that uses
zero for blue, or one that uses a different sequence of colors for
increasing values of hue. The standard I've used has the sequence red,
orange, yellow, green, cyan, blue, violet and back to red for hue values
zero to 1.0. Other color spaces may use other sequences. For example,
hue might be defined as blue, cyan, green, yellow, orange, red, violet
and back to blue for increasing values of hue.

If you need to convert hue color spaces, tricks like dividing by 255,
subtracting the hue from 1.0 (i.e., 1.0 - hue), or adding a value and
using the mod() function will change the hue to a value you can use with
the conversions I've written. All this confusion might explain why
POV-Ray uses the RGB color space. Good luck!

Mark Donovan
Phoenix, Arizona

Josh English wrote:
> Very cool! And very helpful. Since we're on the subject, I am working
> on some images for a friend that are set in a city. They're pretty
> relaxed about how the building are going to look, and in my sample
> scenes they didn't like the sky. I had just used some of the preset
> cloudy skies. They want the sky color to be Hue 205, Saturation 120, and
> Luminosity between 56 and 136. I don't have any way of  converting this
> color! Has anyone seen this color palette? Does anyone know where I
> could find resources too convert it?


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.