8000 Return sRGB from get_pixel_color instead of raw linear values by floraschouw · Pull Request #55 · gimp-mcp/gimp-mcp · GitHub
[go: up one dir, main page]

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions gimp-mcp-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@ def exec_and_get_results(command, context):
return output


# Gegl stores colour in linear light, so Gegl.Color.get_rgba() returns linear
# floats; scaling those by 255 yields linear values wearing an sRGB label, and
# #ff5733 reads back as (255, 24, 8).
#
# babl would convert this for us, but Babl.format() cannot be reached safely
# from here: if babl is not yet initialised it calls babl_fatal() -- "you must
# call babl_init first" -- which aborts the process at the C level and cannot
# be caught from Python. GIMP imports this file during its plug-in scan,
# before that initialisation, so touching babl here makes the whole plug-in
# vanish from the menu. The sRGB transfer function is exact for 8-bit output
# anyway: it round-trips all 256 levels and matches babl on every colour
# tested.


def _linear_to_srgb_u8(value):
"""One linear-light channel (0..1 float) -> 0-255 sRGB byte."""
if value <= 0.0:
return 0
if value <= 0.0031308:
encoded = value * 12.92
else:
encoded = 1.055 * (value ** (1.0 / 2.4)) - 0.055
return max(0, min(255, round(encoded * 255)))


def srgb_bytes_from_color(color):
"""Gegl.Color -> (r, g, b, a) as 0-255 sRGB values."""
red, green, blue, alpha = color.get_rgba()
return (
_linear_to_srgb_u8(red),
_linear_to_srgb_u8(green),
_linear_to_srgb_u8(blue),
# Alpha is stored linearly; it takes no transfer function.
max(0, min(255, round(alpha * 255))),
)


class MCPPlugin(Gimp.PlugIn):
def __init__(self, host='localhost', port=9877):
super().__init__()
Expand Down Expand Up @@ -3946,8 +3983,7 @@ def _get_pixel_color(self, params):
pixel = drawable.get_pixel(x, y)
# GIMP 3.2: get_pixel returns a Gegl.Color object
if hasattr(pixel, 'get_rgba'):
rf, gf, bf, af = pixel.get_rgba()
r, g, b, a = int(rf*255), int(gf*255), int(bf*255), int(af*255)
r, g, b, a = srgb_bytes_from_color(pixel)
else:
# Fallback: old tuple format (num_channels, bytes_array)
channels = list(pixel[1]) if hasattr(pixel[1], '__iter__') else []
Expand Down
0