POV-Ray : Newsgroups : povray.general : Convert number to hex Server Time
26 Apr 2024 02:35:02 EDT (-0400)
  Convert number to hex (Message 1 to 2 of 2)  
From: Mike Horvath
Subject: Convert number to hex
Date: 15 Dec 2016 16:50:54
Message: <5853103e$1@news.povray.org>
I have a JavaScript expression:

"0123456789ABCDEF".charAt((n-n%16)/16) + "0123456789ABCDEF".charAt(n%16)

It is used to convert an integer between 0 and 255 to hexadecimal code.

How do I rewrite this in POV-Ray? Thanks.

Mike


Post a reply to this message

From: Mike Horvath
Subject: Re: Convert number to hex
Date: 15 Dec 2016 17:30:08
Message: <58531970$1@news.povray.org>
On 12/15/2016 4:51 PM, Mike Horvath wrote:
> I have a JavaScript expression:
>
> "0123456789ABCDEF".charAt((n-n%16)/16) + "0123456789ABCDEF".charAt(n%16)
>
> It is used to convert an integer between 0 and 255 to hexadecimal code.
>
> How do I rewrite this in POV-Ray? Thanks.
>
> Mike

Never mind. I forgot I can use arrays instead of strings. Here is some 
code to do it:

#macro CRGB2HEX(Color)
	#local RGBFT = color Color;
	#local R = RGBFT.red;
	#local G = RGBFT.green;
	#local B = RGBFT.blue;
	concat(CDEC2HEX(R), CDEC2HEX(G), CDEC2HEX(B))
#end

#macro CDEC2HEX(n)
	#local HexArray = array[16] 
{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
	#local n = max(0, min(255, round(n * 255, 0)));
	#local Num1 = (n - mod(n, 16))/16;
	#local Num2 = mod(n, 16);
	concat(HexArray[Num1], HexArray[Num2])
#end


Mike


Post a reply to this message

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