What is "Gegl.color" value format?

Hi,
Some GIMP3 APIs demand Gegl.color value as argument. However I can’t find any example of Gegl.color value.
It seems that it is not (255, 255, 255) , (1.0, 1.0, 1.0) or hex format like “#FFFFFF”. What is the value format?

image

This? Gegl.Color

I know that, but just don’t know how to enter any value as Gegl.color. What is the format “GeglColorPrivate*`”? How can we describe white as a Gegl.color, for example?

I have solved the problem by myself.
Solution is as below.

color = Gegl.Color.new(“hex format color number”)

And input this variable “color” as argument of the API.

for example, white is

color = Gegl.Color.new(“#FFFFFF”)

2 Likes

Once you have a color, you can set its components with

color.set_rgba(.1,.2,.3,1.0)  # Linear values
color.set_rgba_with_space(.35,.48,.58,1.0,Babl.space('sRGB'))  # Gamma-encoded values

Similarly you can also get the linear or gamma-encoded RGBA values with get_rgba() and get_rgba_with_space() (which is also a roundabout way to perform linear⇆SRGB conversions).

1 Like

Thank you for your advice!
To initialize variable color, is the following code correct?

color = Gegl.Color.new()

EDIT: @Ofnuts corrected me, you need a parameter.

You can also use some common color names, like
color = Gegl.Color.new("red") or color = Gegl.Color.new("transparent")

Yes, that works.

>>> c=Gegl.Color.new()
Traceback (most recent call last):
  File "/Gimp-dev/3.00/run/lib/x86_64-linux-gnu/gimp/3.0/plug-ins/python-console/pyconsole.py", line 797, in do_command
    eval(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: Gegl.Color.new() takes exactly 1 argument (0 given)

For the names I assume these are the ones I find in the source code?

/* These color names are based on those defined in the HTML 4.01 standard. See
 * http://www.w3.org/TR/html4/types.html#h-6.5
 *
 * Note: these values are stored with gamma
 */
static const ColorNameEntity color_names[] =
{
  { "black",   { 0.f,      0.f,      0.f,      1.f } },
  { "silver",  { 0.75294f, 0.75294f, 0.75294f, 1.f } },
  { "gray",    { 0.50196f, 0.50196f, 0.50196f, 1.f } },
  { "white",   { 1.f,      1.f,      1.f,      1.f } },
  { "maroon",  { 0.50196f, 0.f,      0.f,      1.f } },
  { "red",     { 1.f,      0.f,      0.f,      1.f } },
  { "purple",  { 0.50196f, 0.f,      0.50196f, 1.f } },
  { "fuchsia", { 1.f,      0.f,      1.f,      1.f } },
  { "green",   { 0.f,      0.50196f, 0.f,      1.f } },
  { "lime",    { 0.f,      1.f,      0.f,      1.f } },
  { "olive",   { 0.50196f, 0.50196f, 0.f,      1.f } },
  { "yellow",  { 1.f,      1.f,      0.f,      1.f } },
  { "navy",    { 0.f,      0.f,      0.50196f, 1.f } },
  { "blue",    { 0.f,      0.f,      1.f,      1.f } },
  { "teal",    { 0.f,      0.50196f, 0.50196f, 1.f } },
  { "aqua",    { 0.f,      1.f,      1.f,      1.f } },
  { "none",    { 0.f,      0.f,      0.f,      0.f } },
  { "transparent",  { 0.f, 0.f,      0.f,      0.f } }
};
2 Likes

Huh - I could have swore I did that before in C, but maybe I’m misremembering. Thanks for checking! And yes, those can be used.

EDIT: Ah, right, it’s NULL that works.
GeglColor *color = gegl_color_new (NULL)

There does have to be a parameter. I’m not sure if None would work in Python.

1 Like

Thank you again! Your advice is very helpful!

Well, not in Python it seems:

>>> c=Gegl.Color.new(None)
Traceback (most recent call last):
  File "/Gimp-dev/3.00/run/lib/x86_64-linux-gnu/gimp/3.0/plug-ins/python-console/pyconsole.py", line 797, in do_command
    eval(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: Argument 0 does not allow None as a value

Thank you.

It seems following code is acceptable in Python.

c=Gegl.Color.new(“”)

It looks like the documentation needs some improvements. :wink:

I got how to set the color, tried with hex it worked, but I have no idea how to GET the color.

I tried all these:
color = Gimp.context_get_foreground()
print(color)
float_rgba = color.get_rgba()
print(float_rgba)
float_hsva = color.get_hsva()
print(float_hsva)
float_hsla = color.get_hsla()
print(float_hsla)
float_cmyk = color.get_cmyk()
print(float_cmyk)
print(color.dict)
print(dir(color))
print(vars(color))
print(color.list_properties())

It seems to me there is no way to get the hex rgb, and get_rgba gives something completely different from the hex in gimp. It should be 66,232,141 and I get:
(red=0.05448118969798088, green=0.8069566488265991, blue=0.26635706424713135, alpha=1.0)
(hue=0.40863433480262756, saturation=0.7155155539512634, value=0.9098062515258789, alpha=1.0)
(hue=0.40863433480262756, saturation=0.7830228805541992, lightness=0.5843158960342407, alpha=1.0)
(cyan=0.9324856400489807, magenta=0.0, yellow=0.6699240207672119, key=0.6699240207672119, alpha=0.193043053150177)

These are the linear values (in the [0.0 ... 1.0] range) of the gamma-encoded(*) values (66, 232, 141).

If you want gamma-encoded values in the [0 ... 255] range:

# just showing we are using the same color (same linear values as yours)
>>> fg.get_rgba()
(red=0.054480597376823425, green=0.8069544434547424, blue=0.26635631918907166, alpha=1.0)

# get the gamma-encoded version ([0.0 ... 1.0] range)
fgSrgb=fg.get_rgba_with_space(Babl.space('sRGB'))
>>> fgSrgb
(red=0.25882428884506226, green=0.9098048806190491, blue=0.5529418587684631, alpha=1.0)

# Scale to the  [0 ... 255] range
>>> [x*255 for x in fgSrgb]
[66.00019365549088, 232.0002445578575, 141.0001739859581, 255.0]

Note that you almost never deal with the [0..255] values, unless you want to show/accept them to/from the user.

For some more info: What is the gamma encoding and why are my color computations wrong?

(*) more properly: sRGB-encoded

You are a savior. I was mapping the expected and received and noticed the power law, and down the rabbit hole learning about gamma encoding I found one of your other post explaining this.
You got it exactly right, I need to show the color to the user. More precisely I’m modding for a game where the color encodes regions. So the rgb values end up in a big csv, and I need this to go image > csv.

You’re most welcome. It took me a while to understand this too.