HTML Color Codes

Ken Ray kray at sonsothunder.com
Sat Aug 9 18:12:57 EDT 2008


>>  put baseconvert(r,10,16) into r
>>  set the numberformat to "00"
>>  add 0 to r
>> 
>> This gives you a two-digit number in all cases.
> 
> 
> Will the numberFormat work with hexadecimal numbers?
> 
> That was my mistake in the function Heather, but my solution would be:
>     if the length of r < 2 then put "0" before r
> 
> And repeat this for g & b before assembling the HTML color string.

You can also use the "format" function to not only convert the R, B, and B
numbers to hex, but pad it properly as well!

  put the backColor of this stack into tRGB  -- 194,10,128
  split tRGB by comma
  put format("%02x%02x%02x",tRGB[1],tRGB[2],tRGB[3]) into tHexColor
  put tHexColor
  --> c20a80

And with "do", you can even do it in one line:

  put the backColor of this stack into tRGB  -- 194,10,128
  
  do "put format(" & quote & "%02x%02x%02x" & quote & "," & \
     tRGB & ") into tHexColor
  put tHexColor

  --> c20a80


BTW: format() cool and is great for padding zeroes or spaces in front
things, so you can convert numbers to zero-padded fixed-length 6 digit
numbers like this:

  put 5 into tNum
  put format("%06d",tNum)    --> 000005

Or if you need to pad spaces, you can omit the "0" in the format() call, so:

  put format("%6d",tNum)   -->      5  (5 spaces, then the number "5")

Have fun,

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list