POV-Ray : Newsgroups : povray.text.scene-files : HEX --> RGB : HEX --> RGB Server Time
29 Jul 2024 02:27:27 EDT (-0400)
  HEX --> RGB  
From: Mark Donovan
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

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