Python converting an sRGB value to RGB

75 Views Asked by At

First of all, I PROMISE I've read a ton on this, including here, but nothing seems to come close. I have three values to represent a color, all in sRGB space, and need to convert them to, well, "regular" RGB (where the values go from 0 to 255 in a straight line).

I try this code (recommended many places):

def srgb2lin(s):
   if s <= 0.0404482362771082:
        lin = s / 12.92
    else:
        lin = pow(((s + 0.055) / 1.055), 2.4)
    return lin

And while the values are better, they still are not the same values as I need (example: .4 input returns a 33 whereas I would need a 59, at least in terms of what happens when I manually enter RGB values in a program that generates the sRGB values and then look at it in my program that I need RGB values. If that makes sense).

I get the conversion isn't all that straightforward, but the coding must be available somewhere, right?

1

There are 1 best solutions below

3
Mike Kelley On

Sorry, folks, but I'm an idiot (in my defense, I'm a VERY old programmer - I was working in COBOL before Basic was a thing).

In my routines I had inadvertently mixed up Green and Blue - for some reason (although I KNEW it was "RGB") I had it as RBG. Thus the colors were definitely off... but not due to what I was doing (as we all know, the computer just does what we tell it to do. And I told it to misbehave).

I don't know if there's a way to just delete things here but if so (and if someone else can) please do so. All the "normal" routines to change color space now work just fine in my program.