• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
convert hex colour to RGB values
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

convert hex colour to RGB values


  • Subject: convert hex colour to RGB values
  • From: Hamish Graham <email@hidden>
  • Date: Mon, 19 Jul 2004 00:18:13 +0100

Hi

I've written a little method to convert a hex string into RGB values, however I feel there must be a better way to find the index number of the hex, I have a second method ('getIndex()') to do it for me...

Is there a nice way to get the position of say "D" in a list like:
{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

below are my methods,


--
-- converts a hex string (e.g "#00CCFF") to RGB values in the range 0 -> 65535
--
on HexToRGB(hexValue)
set hexValue to text 2 through (count of hexValue) of hexValue -- strip the '#"
set RGBsmall to {0, 0, 0} -- range 0 -> 255, RGBsmall is here to check the result more easily
set RGBbig to {0, 0, 0} -- range 0 -> 65535

repeat with k from 1 to count of hexValue by 2
set hex to text k through (k + 1) of hexValue
set numX to getIndex(first character of hex)
set numY to getIndex(second character of hex)

set dec to ((numX * (16 ^ 1) + numY * (16 ^ 0)))

set item ((k div 2) + 1) of RGBsmall to dec
set item ((k div 2) + 1) of RGBbig to ((dec + 1) * 256) - 1

end repeat

log RGBsmall
log RGBbig

return RGBbig
end HexToRGB


on getIndex(val)
set hexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set indexNum to 0
repeat with s from 1 to count of hexList
if item s of hexList is equal to val then
set indexNum to s - 1
exit repeat
end if
end repeat

return indexNum
end getIndex




Thanks
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: convert hex colour to RGB values
      • From: Graff <email@hidden>
  • Prev by Date: Re: Long Filenames >31 error -37
  • Next by Date: Re: Avoiding repetition
  • Previous by thread: Re: Scripting with two windows in BBEdit
  • Next by thread: Re: convert hex colour to RGB values
  • Index(es):
    • Date
    • Thread