: Is it possible to revert preapplied alpha blend and find original color? I have a color 0xADADAD, it was originally white, now it's grey, because it's been alpha blended with 30%(0.3 alpha)
I have a color 0xADADAD, it was originally white, now it's grey, because it's been alpha blended with 30%(0.3 alpha) black + the unknown original value. In this case I know from lots of trial and error that the original color was 0xF8F8F8
Problem is I have over 20 other colors that need to be fixed like this, but I have no accurate way of finding the original colors. Is there a formula I can do to figure this out? Is it even possible?
More posts by @Sims5801359
1 Comments
Sorted by latest first Latest Oldest Best
Yeah its possible i made a quick change to the code in this post and came up with following very sloppy python 2.X code:
from Tkinter import *
import struct
def ablend(a, fg, bg):
return ((1-a)*fg[0]+a*bg[0],
(1-a)*fg[1]+a*bg[1],
(1-a)*fg[2]+a*bg[2])
def aunblend(a, result, bg):
return ((result[0]-a*bg[0])/(1-a),
(result[1]-a*bg[1])/(1-a),
(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 = '0.3'
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',data[1:-1].decode('hex'))
self.e.configure(background=data[:-1])
self.bg = struct.unpack('BBB',data2[1:-1].decode('hex'))
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()
And you get a window that allows you to type alpha, resulting color and background.
Image 1: Dialog of program
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.