Page 1 of 2

Extract color code

Posted: Wed Feb 16, 2022 8:28 pm
by SEAL29
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.

Re: Extract color code

Posted: Wed Feb 16, 2022 9:01 pm
by dunbarx
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

Re: Extract color code

Posted: Wed Feb 16, 2022 9:03 pm
by Klaus
Do you mean this?

Code: Select all

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

Re: Extract color code

Posted: Wed Feb 16, 2022 10:55 pm
by bn
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

Re: Extract color code

Posted: Thu Feb 17, 2022 12:10 pm
by bn
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

Re: Extract color code

Posted: Thu Feb 17, 2022 2:19 pm
by richmond62
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.

Re: Extract color code

Posted: Thu Feb 17, 2022 2:54 pm
by richmond62

Re: Extract color code

Posted: Thu Feb 17, 2022 3:15 pm
by bn
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

Re: Extract color code

Posted: Thu Feb 17, 2022 3:23 pm
by SEAL29
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.

Re: Extract color code

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

Code: Select all

answer color
is going to give an RGB readout.

Re: Extract color code

Posted: Thu Feb 17, 2022 3:44 pm
by bn
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

Re: Extract color code

Posted: Thu Feb 17, 2022 4:29 pm
by FourthWorld
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

Re: Extract color code

Posted: Thu Feb 17, 2022 4:30 pm
by richmond62
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.

Re: Extract color code

Posted: Thu Feb 17, 2022 6:08 pm
by FourthWorld
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.

Re: Extract color code

Posted: Thu Feb 17, 2022 6:19 pm
by richmond62
And what about:

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

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