0

I have a color that has a HEX value of 180031 or rgb(24, 0, 49). However, the color is supposed to be a shade of purple with a black background, but I don't know what the alpha value is.

Is there a program that could help determine the alpha value of a color?

I've done some digging here and I found that this post has a Python program, but it doesn't seem to work with later versions of Python.

2
  • 1
    so the math itself within the functions ablendand aunblend in the linked Q&A is enough for you to do the calculation even by hand, I am not sure you have enough information here: leaving aside the possibility the G channel 00 is "clipped," you basically can derive your result color using any alpha value (or at least maybe .15-1.0). For example If you calculate using .3 alpha and .8 alpha, you get two sets of values that, when applied and set the appropriate alpha over black result in the same (blended) color
    – Yorik
    Jan 25, 2023 at 21:37
  • If it's always blended on top of black, then just divide your highest component (49 blue) by the same component of your base purple color. ... (You kids need a program for everything nowadays :-)) Feb 26, 2023 at 17:24

1 Answer 1

0

ported referenced post to python 3, there is still a small rounding error due to type coercion differences might fix later:

from tkinter import *
import struct


def ablend(a, fg, bg):
    return (int( (1-a)*fg[0]+a*bg[0] ),
            int( (1-a)*fg[1]+a*bg[1] ),
            int( (1-a)*fg[2]+a*bg[2] ) )

def aunblend(a, result, bg):
    return (int((result[0]-a*bg[0])/(1-a)),
            int((result[1]-a*bg[1])/(1-a)),
            int((result[2]-a*bg[2])/(1-a)))

class findUnblendedColor(object):

    def __init__(self, root):
        self.fg=(0xAD, 0xAD, 0xAD)
        self.bg=(255,255,255)
        self.alpha = 0.3

        l = Label(root, text="alpha:")
        l.grid(row=0, column=0)
        label = str(self.alpha)
        self.a = Text(root, height=1,
                       relief=FLAT,
                       width=20,)
        self.a.insert(1.0, label)
        self.a.grid(row=0, column=1)
        self.a.bind("<Return>", self.update)

        l = Label(root, text="Result:")
        l.grid(row=1, column=0)
        label = '#%02x%02x%02x' % self.fg
        self.e = Text(root, height=1,
                       relief=FLAT,
                       width=20,
                       background=label)
        self.e.insert(1.0, label)
        self.e.grid(row=1, column=1)
        self.e.bind("<Return>", self.update)

        l = Label(root, text="BG:")
        l.grid(row=2, column=0)

        label = '#%02x%02x%02x' % self.bg
        self.b = Text(root, height=1,
                       relief=FLAT,
                       width=20,
                       background=label)
        self.b.insert(1.0, label)
        self.b.grid(row=2, column=1)
        self.b.bind("<Return>", self.update)


        l = Label(root, text="FG:")
        l.grid(row=3, column=0)

        col = aunblend(self.alpha, self.fg, self.bg)
        label = '#%02x%02x%02x' % col
        self.f = Text(root, height=1,
                       relief=FLAT,
                       width=20,
                       background=label)
        self.f.insert(1.0, label)
        self.f.configure(state='disabled')
        self.f.grid(row=3, column=1)


        l = Label(root, text="verify:")
        l.grid(row=4, column=0)

        col = ablend(self.alpha, col, self.bg)
        label = '#%02x%02x%02x' % col
        self.g = Text(root, height=1,
                       relief=FLAT,
                       width=20,
                       background=label)
        self.g.insert(1.0, label)
        self.g.configure(state='disabled')
        self.g.grid(row=4, column=1)



    def update(self, event):
        data = self.e.get(1.0,END)
        self.e.delete(1.0, END)
        self.e.insert(END, data[:-1])

        data2 = self.b.get(1.0,END)
        self.b.delete(1.0, END)
        self.b.insert(END, data2[:-1])

        try:
            self.alpha = float(self.a.get(1.0,END)) 

            self.fg = struct.unpack( 'BBB', bytes.fromhex(data[1:-1]) )
            self.e.configure(background=data[:-1])


            self.bg = struct.unpack( 'BBB',bytes.fromhex(data2[1:-1]) )
            self.b.configure(background=data2[:-1])

            col = aunblend(self.alpha, self.fg, self.bg)
            self.f.configure(state='normal')
            self.g.configure(state='normal')

            if all(i <= 256 and i >= 0 for i in col):
                label = '#%02x%02x%02x' % col
                self.f.configure(background=label)

                col = ablend(self.alpha, col, self.bg)
                label2 = '#%02x%02x%02x' % col
                self.g.configure(background=label2)
            else:
                label = "out of range"
                label2 = "out of range"
            self.f.delete(1.0, END)
            self.f.insert(END, label)
            self.g.delete(1.0, END)
            self.g.insert(END, label2)

            self.f.configure(state='disabled')
            self.g.configure(state='disabled')
        except Exception as ex:
            print("error "+repr(type(ex)))
            pass

        return 'break'

root = Tk()
root.title("unblend")
findUnblendedColor(root)

root.mainloop()

Also noticed that the original code probably has a small error in it but didn't bother to fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.