return hex value of chosen color

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

return hex value of chosen color

Post by ThomasBodetti » Mon Sep 16, 2013 9:16 pm

Hello, I am looking at how to return the chosen color of a value in its Hex value

Code: Select all

 get the backgroundcolor of control ID "1003" 
   put it into myColorVar
   put myColorVar into fld "colorID"
This will create an RGB value, like 22,183,67 for a blue

how would I get the hex value of that same value, like this, #0000FF

Thanks for any guidence and or suggestions

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: return hex value of chosen color

Post by Simon » Mon Sep 16, 2013 9:27 pm

Here you go :)

Code: Select all

   repeat with x =1 to the number of items in myColorVar
      put baseconvert(item x of myColorVar,10,16) after tHex
   end repeat
Fun!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: return hex value of chosen color

Post by Simon » Mon Sep 16, 2013 9:41 pm

Ok more accurately:

Code: Select all

   repeat with x =1 to the number of items in myColorVar
      put baseconvert(item x of myColorVar,10,16) into tHex
      if the number of chars in tHex < 2 then
         put "0" before tHex
      end if
      put tHex after myHexColorVar
   end repeat
   put "#" before myHexColorVar
That will make it look the way you want.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ThomasBodetti
Posts: 16
Joined: Tue May 14, 2013 12:30 pm

Re: return hex value of chosen color

Post by ThomasBodetti » Mon Sep 16, 2013 9:45 pm

Thank you Simon, That works perfectly.

They should add that to the dictionary and manual.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: return hex value of chosen color

Post by Simon » Tue Sep 17, 2013 1:54 am

Hi Thomas,
Actually I didn't know how to do it but the word "convert" popped into my head, 'cause it's what you were asking for. Just a search on "convert" in the dictionary showed me how to do it.

The English-like language of liveCode makes it easier to look up ideas and find answers.

At any rate, glad I could help you.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply