Extract color code

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

SEAL29
Posts: 63
Joined: Fri Oct 02, 2020 3:32 pm

Extract color code

Post by SEAL29 » Wed Feb 16, 2022 8:28 pm

How do i extract the HTML or RGB color code from system default color chooser?

Code: Select all

answer color
set the backgroundColor of graphic "tester" to it
But i like extract the colors code into filed, example #C0808080 in field.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Extract color code

Post by dunbarx » Wed Feb 16, 2022 9:01 pm

I am not sure what you are asking.

On a new card, make a new field. The backColor of that field is likely to be empty, since it inherits the color of the card, which probably has none. Open the "colors" tab of the inspector, and select "backGround fill". Bring the slider from full black all the way left and hit "OK". It looks like nothing has changed, but if you ask for the backColor of the field you get "255,255,255" or "white".

There are several colors associated with a field, like the "foreColor", which is the color of the text in that field.

Say again what you are asking for.

Craig

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Extract color code

Post by Klaus » Wed Feb 16, 2022 9:03 pm

Do you mean this?

Code: Select all

answer color
## set the backgroundColor of graphic "tester" to it
put IT into fld "the chosen color"

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Extract color code

Post by bn » Wed Feb 16, 2022 10:55 pm

It looks like Seal29 is looking for HTML hex colors.

To do this:

Code: Select all

on mouseUp
   answer color
   if it is empty then exit mouseUp
   --   it contains the color as RGB, for example (0,0,0) black
   repeat for each item aDezimalColor in it
      put format("%02s",(baseConvert(aDezimalColor, 10, 16))) after tResult
   end repeat
   put "#" before tResult
   --   tResult contains the color as HTML Hex, for example (#000000) black
   -- set the backgroundColor of grc 1 to tResult -- you can set the colors in LC via HTML hex colors
   put tResult into field 1
end mouseUp
I tested it with a web HTML color tester and by setting the background color of a graphic and it worked. No guarantees though...

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Extract color code

Post by bn » Thu Feb 17, 2022 12:10 pm

It turns out that the conversion of a RGB color to a Hex color can be simplified. It helps reading the dictionary entry for "format" carefully. :D

Here is a function that returns a Hex color when you pass it a RGB color (like 255,0,127)

Code: Select all

function ConvertRGBToHexColor pRGB
   local tHexColor
   put "#" into tHexColor
   repeat with j = 1 to 3
      put format("%02X",item j of pRGB) after tHexColor
   end repeat
   return tHexColor
end ConvertRGBToHexColor
Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Extract color code

Post by richmond62 » Thu Feb 17, 2022 2:19 pm

Oddly enough . . .
-
SShot 2022-02-17 at 15.15.49.png
-
You can set the backgroundColor using an HTML-style colour.

The snag is that there does not seem to be a way to read the backgroundColor directly
in HTML-style format.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Extract color code

Post by richmond62 » Thu Feb 17, 2022 2:54 pm


bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Extract color code

Post by bn » Thu Feb 17, 2022 3:15 pm

Richmond,
richmond62 wrote:
Thu Feb 17, 2022 2:19 pm
Oddly enough . . .
You can set the backgroundColor using an HTML-style colour.
The snag is that there does not seem to be a way to read the backgroundColor directly
in HTML-style format.
If you set the backgroundColor of a graphic via a RGB-triplet you will get a RGB-triplet when trying to read the backgroundColor.
If you set the backgroundColor of a graphic via an HTML Hex-color you will get a HTML Hex-color when trying to read the backgroundColor.

To convert from an HTML Hex-color to RGB then you can use the old trick using the backgroundPixel as mentioned in Tim Bobo's lesson

Code: Select all

 set the backgroundPixel of grc 1 to the backgroundPixel of grc 1
 put the backgroundColor of grc 1 into tColor
It returns an RGB triplet. This also works for named colors.

Kind regards
Bernd

SEAL29
Posts: 63
Joined: Fri Oct 02, 2020 3:32 pm

Re: Extract color code

Post by SEAL29 » Thu Feb 17, 2022 3:23 pm

bn wrote:
Wed Feb 16, 2022 10:55 pm
It looks like Seal29 is looking for HTML hex colors.

To do this:

Code: Select all

on mouseUp
   answer color
   if it is empty then exit mouseUp
   --   it contains the color as RGB, for example (0,0,0) black
   repeat for each item aDezimalColor in it
      put format("%02s",(baseConvert(aDezimalColor, 10, 16))) after tResult
   end repeat
   put "#" before tResult
   --   tResult contains the color as HTML Hex, for example (#000000) black
   -- set the backgroundColor of grc 1 to tResult -- you can set the colors in LC via HTML hex colors
   put tResult into field 1
end mouseUp
I tested it with a web HTML color tester and by setting the background color of a graphic and it worked. No guarantees though...

Kind regards
Bernd
That's it, thank you everyone.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Extract color code

Post by richmond62 » Thu Feb 17, 2022 3:41 pm

BUT . . .

Code: Select all

answer color
is going to give an RGB readout.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Extract color code

Post by bn » Thu Feb 17, 2022 3:44 pm

richmond62 wrote:
Thu Feb 17, 2022 3:41 pm
BUT . . .

Code: Select all

answer color
is going to give an RGB readout.
Richmond,

I don't quite understand what you are referring to. The OP apparently wanted a way to convert a RGB-triplet to a HTML Hex color.

Kind regards
Bernd

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Extract color code

Post by FourthWorld » Thu Feb 17, 2022 4:29 pm

If this is for web generation, SEAL29 can also use the decimal RGB values directly (without hex conversion) with the CSS rgb() method:
https://www.w3schools.com/htmL/html_colors_rgb.asp
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Extract color code

Post by richmond62 » Thu Feb 17, 2022 4:30 pm

If you set the backgroundColor of a graphic via an HTML Hex-color you will get a HTML Hex-color when trying to read the backgroundColor.
What I am refering to (and this may be beside the OP's point [but, as you know, I have never let that get in my way])
is that unless a backgroundColor has been explicitly set using an HTML hex colour

Code: Select all

answer color
will NOT return an HTML hex code, and there seems no way, in LiveCode, to force things so that happens.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Extract color code

Post by FourthWorld » Thu Feb 17, 2022 6:08 pm

True, LC uses one consistent format for representing colors internally, exposed to us as a comma-delimited triplet of integers.

But that doesn't stop us from making a function to convert that form to any other, as Bernd has shown.

And conversion may not even be needed, depending on the goal of the exercise, as the CSS rgb() method shows.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Extract color code

Post by richmond62 » Thu Feb 17, 2022 6:19 pm

And what about:

https://www.rapidtables.com/convert/col ... -cmyk.html

That page gives all the Maths to go from RGB to CMYK.

Post Reply