POV-Ray : Newsgroups : povray.text.scene-files : HSL --> RGB Server Time
1 Nov 2024 01:25:43 EDT (-0400)
  HSL --> RGB (Message 1 to 6 of 6)  
From: Theran Cochran
Subject: HSL --> RGB
Date: 10 Oct 1998 17:10:52
Message: <361F786D.C00EBA01@foto.infi.net>
I'm thinking about creating a macro to convert HSL colors to RGB. I want
to know if someone has already done this so I won't be wasting my time
(it does seem to me like a quite obvious use of macros). If it hasn't
been done, then could you please direct me to some info on the methods
involved, or even a snippet of code. Thanks!

-- 

Theran Cochran - shi### [at] fotoinfinet
http://members.xoom.com/theran/


Post a reply to this message

From: Mark Donovan
Subject: Re: HSL --> RGB
Date: 11 Oct 1998 16:23:01
Message: <36210591.8D303C9D@worldnet.att.net>
I've done it. Here's a version of HSL to RGB and also a macro for HSB to
RGB. The descriptions explain the difference. These versions work
without trig or vector functions. To be complete, I've tacked on RGB to
HSL and RGB to HSB conversions.

Theran Cochran wrote:
> 
> I'm thinking about creating a macro to convert HSL colors to RGB. I want
> to know if someone has already done...

Mark Donovan
Phoenix, Arizona, USA
===
#local Version_Temp = version;
#version 3.1;

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

/*
   Convert HSL to RGB color space
   Date:   July 1998
   Author: Mark Donovan

 Description:

   Converts hue, saturation, and lightness a POV-Ray color. Each component
   is defined in the range 0.0 to 1.0. Hue value 0.0 is red. Fully saturated
   color is defined at lightness 0.5 and saturation 1.0.
   No test for values out of range.

 Typical call:

   #include "hsl2rgb.inc"
   // HSL is a vector <hue, saturation, lightness>
   #declare RGB = hsl2rgb(<0.743, 0.234, 0.123>);
   // returns POV-Ray color RGB = color rgb <red, green, blue>

*/

#macro hsl2rgb(HSL)
#local _HSL_H = HSL.x;
#local _HSL_S = HSL.y;
#local _HSL_L = HSL.z;

#local _HSL_Sextant = int(6.0 * _HSL_H);
#if (_HSL_L <= 0.5)
  #local _HSL_M = _HSL_L * (1.0 + _HSL_S);
#else
  #local _HSL_M = _HSL_L + _HSL_S - _HSL_L * _HSL_S;
#end
#local _HSL_m = 2.0 * _HSL_L - _HSL_M;
#if (_HSL_S = 0.0)
  #local _HSL_R = _HSL_L;
  #local _HSL_G = _HSL_L;
  #local _HSL_B = _HSL_L;
#else
  #switch (_HSL_Sextant)
  #case (0)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m + (_HSL_M - _HSL_m) * _HSL_H * 6.0;
    #local _HSL_B = _HSL_m;
  #break
  #case (1)
    #local _HSL_R = _HSL_m + (_HSL_M - _HSL_m) * (2.0 - 6.0 * _HSL_H);
    #local _HSL_G = _HSL_M;
    #local _HSL_B = _HSL_m;
  #break
  #case (2)
    #local _HSL_R = _HSL_m;
    #local _HSL_G = _HSL_M;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 * _HSL_H - 2.0);
  #break
  #case (3)
    #local _HSL_R = _HSL_m;
    #local _HSL_G = _HSL_m + (_HSL_M - _HSL_m) * (4.0 - 6.0 * _HSL_H);
    #local _HSL_B = _HSL_M;
  #break
  #case (4)
    #local _HSL_R = _HSL_m + (_HSL_M - _HSL_m) * (6.0 * _HSL_H - 4.0);
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_M;
  #break
  #case (5)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 - 6.0 * _HSL_H);
  #break
  #case (6)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 - 6.0 * _HSL_H);
  #break
  #else
    #error "bad HSL conversion"
  #end
#end
color rgb <_HSL_R, _HSL_G, _HSL_B>
#end

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

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

/*
   Convert HSB to RGB color space
   Date:   July 1998
   Author: Mark Donovan

 Description:

   Converts hue, saturation, and value to a POV-Ray color. Each component
   is defined in the range 0.0 to 1.0. Hue value 0.0 is red. Fully saturated
   color is defined at brightness 1.0 and saturation 1.0. No test for values
   out of range.

 Typical call:

   #include "hsb2rgb.inc"
   // HSB is a vector
   #declare RGB = hsb2rgb(<0.743, 0.234, 0.123>);
   // returns POV-Ray color RGB = color rgb <red, green, blue>

*/

#macro hsb2rgb(HSB)
#local _HSB_H = HSB.x;
#local _HSB_S = HSB.y;
#local _HSB_V = HSB.z;

// HSB to RGB conversion
#if (_HSB_S = 0.0)
  #local _HSB_R = _HSB_V;
  #local _HSB_G = _HSB_V;
  #local _HSB_B = _HSB_V;
#else
  #if (_HSB_H >= 1.0)  // red is red
    #declare _HSB_H = 0.0;
  #end
  #local _HSB_h = _HSB_H * 6.0;
  #local _HSB_i = int(_HSB_h);
  #local _HSB_f = _HSB_h - _HSB_i;
  #local _HSB_p = _HSB_V * (1.0 - _HSB_S);
  #local _HSB_q = _HSB_V * (1.0 - (_HSB_S * _HSB_f));
  #local _HSB_t = _HSB_V * (1.0 - (_HSB_S * (1.0 - _HSB_f)));
  #switch (_HSB_i)
  #case (0)
    #local _HSB_R = _HSB_V;
    #local _HSB_G = _HSB_t;
    #local _HSB_B = _HSB_p;
  #break
  #case (1)
    #local _HSB_R = _HSB_q;
    #local _HSB_G = _HSB_V;
    #local _HSB_B = _HSB_p;
  #break
  #case (2)
    #local _HSB_R = _HSB_p;
    #local _HSB_G = _HSB_V;
    #local _HSB_B = _HSB_t;
  #break
  #case (3)
    #local _HSB_R = _HSB_p;
    #local _HSB_G = _HSB_q;
    #local _HSB_B = _HSB_V;
  #break
  #case (4)
    #local _HSB_R = _HSB_t;
    #local _HSB_G = _HSB_p;
    #local _HSB_B = _HSB_V;
  #break
  #case (5)
    #local _HSB_R = _HSB_V;
    #local _HSB_G = _HSB_p;
    #local _HSB_B = _HSB_q;
  #break
  #else
    #error "bad HSB conversion"
  #end
#end
color rgb <_HSB_R, _HSB_G, _HSB_B>
#end

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

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

/*
   Convert HSL to RGB color space
   Date:   July 1998
   Author: Mark Donovan

 Description:

   Converts hue, saturation, and lightness a POV-Ray color. Each component
   is defined in the range 0.0 to 1.0. Hue value 0.0 is red. Fully saturated
   color is defined at lightness 0.5 and saturation 1.0.
   No test for values out of range.

 Typical call:

   #include "hsl2rgb.inc"
   // HSL is a vector <hue, saturation, lightness>
   #declare RGB = hsl2rgb(<0.743, 0.234, 0.123>);
   // returns POV-Ray color RGB = color rgb <red, green, blue>

*/

#macro hsl2rgb(HSL)
#local _HSL_H = HSL.x;
#local _HSL_S = HSL.y;
#local _HSL_L = HSL.z;

#local _HSL_Sextant = int(6.0 * _HSL_H);
#if (_HSL_L <= 0.5)
  #local _HSL_M = _HSL_L * (1.0 + _HSL_S);
#else
  #local _HSL_M = _HSL_L + _HSL_S - _HSL_L * _HSL_S;
#end
#local _HSL_m = 2.0 * _HSL_L - _HSL_M;
#if (_HSL_S = 0.0)
  #local _HSL_R = _HSL_L;
  #local _HSL_G = _HSL_L;
  #local _HSL_B = _HSL_L;
#else
  #switch (_HSL_Sextant)
  #case (0)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m + (_HSL_M - _HSL_m) * _HSL_H * 6.0;
    #local _HSL_B = _HSL_m;
  #break
  #case (1)
    #local _HSL_R = _HSL_m + (_HSL_M - _HSL_m) * (2.0 - 6.0 * _HSL_H);
    #local _HSL_G = _HSL_M;
    #local _HSL_B = _HSL_m;
  #break
  #case (2)
    #local _HSL_R = _HSL_m;
    #local _HSL_G = _HSL_M;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 * _HSL_H - 2.0);
  #break
  #case (3)
    #local _HSL_R = _HSL_m;
    #local _HSL_G = _HSL_m + (_HSL_M - _HSL_m) * (4.0 - 6.0 * _HSL_H);
    #local _HSL_B = _HSL_M;
  #break
  #case (4)
    #local _HSL_R = _HSL_m + (_HSL_M - _HSL_m) * (6.0 * _HSL_H - 4.0);
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_M;
  #break
  #case (5)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 - 6.0 * _HSL_H);
  #break
  #case (6)
    #local _HSL_R = _HSL_M;
    #local _HSL_G = _HSL_m;
    #local _HSL_B = _HSL_m + (_HSL_M - _HSL_m) * (6.0 - 6.0 * _HSL_H);
  #break
  #else
    #error "bad HSL conversion"
  #end
#end
color rgb <_HSL_R, _HSL_G, _HSL_B>
#end

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

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

/*
   Convert HSB to RGB color space
   Date:   July 1998
   Author: Mark Donovan

 Description:

   Converts hue, saturation, and value to a POV-Ray color. Each component
   is defined in the range 0.0 to 1.0. Hue value 0.0 is red. Fully saturated
   color is defined at brightness 1.0 and saturation 1.0. No test for values
   out of range.

 Typical call:

   #include "hsb2rgb.inc"
   // HSB is a vector
   #declare RGB = hsb2rgb(<0.743, 0.234, 0.123>);
   // returns POV-Ray color RGB = color rgb <red, green, blue>

*/

#macro hsb2rgb(HSB)
#local _HSB_H = HSB.x;
#local _HSB_S = HSB.y;
#local _HSB_V = HSB.z;

// HSB to RGB conversion
#if (_HSB_S = 0.0)
  #local _HSB_R = _HSB_V;
  #local _HSB_G = _HSB_V;
  #local _HSB_B = _HSB_V;
#else
  #if (_HSB_H >= 1.0)  // red is red
    #declare _HSB_H = 0.0;
  #end
  #local _HSB_h = _HSB_H * 6.0;
  #local _HSB_i = int(_HSB_h);
  #local _HSB_f = _HSB_h - _HSB_i;
  #local _HSB_p = _HSB_V * (1.0 - _HSB_S);
  #local _HSB_q = _HSB_V * (1.0 - (_HSB_S * _HSB_f));
  #local _HSB_t = _HSB_V * (1.0 - (_HSB_S * (1.0 - _HSB_f)));
  #switch (_HSB_i)
  #case (0)
    #local _HSB_R = _HSB_V;
    #local _HSB_G = _HSB_t;
    #local _HSB_B = _HSB_p;
  #break
  #case (1)
    #local _HSB_R = _HSB_q;
    #local _HSB_G = _HSB_V;
    #local _HSB_B = _HSB_p;
  #break
  #case (2)
    #local _HSB_R = _HSB_p;
    #local _HSB_G = _HSB_V;
    #local _HSB_B = _HSB_t;
  #break
  #case (3)
    #local _HSB_R = _HSB_p;
    #local _HSB_G = _HSB_q;
    #local _HSB_B = _HSB_V;
  #break
  #case (4)
    #local _HSB_R = _HSB_t;
    #local _HSB_G = _HSB_p;
    #local _HSB_B = _HSB_V;
  #break
  #case (5)
    #local _HSB_R = _HSB_V;
    #local _HSB_G = _HSB_p;
    #local _HSB_B = _HSB_q;
  #break
  #else
    #error "bad HSB conversion"
  #end
#end
color rgb <_HSB_R, _HSB_G, _HSB_B>
#end

#version Version_Temp;
===


Post a reply to this message

From: Ken
Subject: Re: HSL --> RGB
Date: 11 Oct 1998 21:48:06
Message: <362151AC.6CCBFF19@pacbell.net>
Mark Donovan wrote:

> I've done it. Here's a version of HSL to RGB and also a macro for HSB to
> RGB. The descriptions explain the difference. These versions work
> without trig or vector functions. To be complete, I've tacked on RGB to
> HSL and RGB to HSB conversions.
>

Very nice indeed. I have needed script like this for some time.
Any chance of doing a hex2rgb script ?

Ken Tyler


Post a reply to this message

From: Mark Donovan
Subject: Re: HSL --> RGB
Date: 12 Oct 1998 05:11:42
Message: <3621B9C0.133D0681@worldnet.att.net>
Seriously... hex? Let me ponder on that for a bit. Maybe it's possible.

Mark

Ken wrote:
> Very nice indeed. I have needed script like this for some time.
> Any chance of doing a hex2rgb script ?


Post a reply to this message

From: Ken
Subject: Re: HSL --> RGB
Date: 12 Oct 1998 07:42:00
Message: <3621DCDE.49FB80C6@pacbell.net>
Mark Donovan wrote:

> Seriously... hex? Let me ponder on that for a bit. Maybe it's possible.
>
> Mark
>
> Ken wrote:
> > Very nice indeed. I have needed script like this for some time.
> > Any chance of doing a hex2rgb script ?

    No real rush. There are a lot of custom/unique colors available
on the web as hex code for html page design. I think it would come
down to having to convert hex to decimal using #declares and then
convert from there. That would mean a lot of declarations. Like I
said there is no real rush and for that matter there are several
hex to decimal converters available in the free/shareware market.
That in it self makes it possible with the scripts you have provided.

Ken Tyler


Post a reply to this message

From: Theran Cochran
Subject: Re: HSL --> RGB
Date: 15 Oct 1998 00:01:40
Message: <36251EDE.D81BD407@foto.infi.net>
Ken wrote:
> Any chance of doing a hex2rgb script ?

Well, thats something I do know how to do! Gotta love them macros.

(BTW: Thanks for signing my guestbook!)

// Persistence of Vision Ray Tracer Scene Description File
// File: hex2rgb.pov
// Vers: 3.1
// Desc: Convert HTML style hex colors into POV RGB vectors
// Date: Wednesday, October 14, 1998 
// Auth: Theran Cochran

#macro Hex2Int(hex)

	#local curStr = strlwr(hex)
	
	#local zeroChar = asc("0");
	#local nineChar = asc("9");
	#local aChar = asc("a");
	#local fChar = asc("f");
	
	#local fin = false;
	#local value = 0;
	#local temp = 0;
	#local isNum = false;
	
	#while(!fin)
		#local curChar = asc(curStr);
		
		#if((curChar >= zeroChar) & (curChar <= nineChar))
			#local isNum = true;
			#local temp = curChar - zeroChar;	
			#local value = value*16 + temp;
		#else 
			#if((curChar >= aChar) & (curChar <= fChar))
				#local isNum = true;
				#local temp = 10 + curChar - aChar;	
				#local value = value*16 + temp;
			#else
				#local fin = true;
			#end
		#end
		
		#if(strlen(curStr))
			#local curStr = substr(curStr, 2, strlen(curStr) -1)
		#else 
			#local fin = true;
		#end
		
	#end

	#if(!isNum) #local value= -1; #end

	value

#end

#macro IndexOf2(string, sstring, i)
	
	#local stringlen = strlen(string);
	#local sstringlen = strlen(sstring);
	#local fin = false;
	#local index = i;
	
	#if(stringlen < sstringlen)
		#local index = -1;
		#local fin = true;
	#end
	#if(sstringlen <= 0)
		#local index = -1;
		#local fin = true;
	#end
	
	
	#while(!fin)
		#if(index > stringlen - sstringlen)
			#local index = -1
			#local fin = true;
		#end

		#if(strcmp(substr(string, index + 1, sstringlen), sstring) = 0)
			#local fin = true;
		#end
		
		
		#local index = index + 1;
	#end
	
	index - 1
	
#end

#macro IndexOf(string, sstring)

	IndexOf2(string, sstring, 0)

#end

#macro Hex2RGB(string)
#local warn = false;

#if(strlen(string) < 7)
#local warn = true;
#else
	#local redval = Hex2Int(substr(string, 2, 2))
	#if(redval < 0) #local warn = true; #end
	#local greenval = Hex2Int(substr(string, 4, 2))
	#if(greenval < 0) #local warn = true; #end
	#local blueval = Hex2Int(substr(string, 6, 2))
	#if(blueval < 0) #local warn = true; #end

#end

#if(warn)
	#warning concat("\nWARNING: Hex2RGB #macro: Bad hex value: \"", string, "\"\n")
#end

<redval/255, greenval/255, blueval/255>

#end

//Begin test scene

#declare camera_location = <1, 3, -5>;
#declare camera_look_at = <0, 0, 0>;
#declare camera_angle = 45;

camera {
	location camera_location
	look_at camera_look_at
	angle camera_angle
}

light_source { 0, color rgb <1, 1, 1>*1.25
//	area_light x*50, z*50, 3, 3
	adaptive 0
	jitter
	translate y*1000
	rotate <-30, -45, 0>
}

sphere { 0, 1 pigment { color Hex2RGB("#4321af") } }

//<---- End of hexrgb.pov

-- 

Theran Cochran - shi### [at] fotoinfinet
http://members.xoom.com/theran/


Post a reply to this message

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