image_tone_up / utils /ColorMatrix.py
Suprhimp
first commit
3450e6b
raw
history blame
No virus
6.11 kB
import numpy as np
class ColorMatrix:
def __init__(self, a=1, b=0, c=0, d=0, e=0, f=0, g=1, h=0, i=0, j=0, k=0, l=0, m=1, n=0, o=0, p=0, q=0, r=0, s=1, t=0):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
self.h = h
self.i = i
self.j = j
self.k = k
self.l = l
self.m = m
self.n = n
self.o = o
self.p = p
self.q = q
self.r = r
self.s = s
self.t = t
self.matrix = np.array([
[a, b, c, d, e],
[f, g, h, i, j],
[k, l, m, n, o],
[p, q, r, s, t],
[0, 0, 0, 0, 1]
])
def apply(self, color):
r = self.a * color.r + self.b * color.g + \
self.c * color.b + self.d * color.a + self.e
g = self.f * color.r + self.g * color.g + \
self.h * color.b + self.i * color.a + self.j
b = self.k * color.r + self.l * color.g + \
self.m * color.b + self.n * color.a + self.o
a = self.p * color.r + self.q * color.g + \
self.r * color.b + self.s * color.a + self.t
return Color(r, g, b, a)
def reset(self):
self.a = 1
self.b = 0
self.c = 0
self.d = 0
self.e = 0
self.f = 0
self.g = 1
self.h = 0
self.i = 0
self.j = 0
self.k = 0
self.l = 0
self.m = 1
self.n = 0
self.o = 0
self.p = 0
self.q = 0
self.r = 0
self.s = 1
self.t = 0
self.matrix = np.array([
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]
])
def multiply(self, other):
E = other
R = self
t = E.a * R.a + E.b * R.f + E.c * R.k + E.d * R.p
i = E.a * R.b + E.b * R.g + E.c * R.l + E.d * R.q
o = E.a * R.c + E.b * R.h + E.c * R.m + E.d * R.r
n = E.a * R.d + E.b * R.i + E.c * R.n + E.d * R.s
a = E.f * R.a + E.g * R.f + E.h * R.k + E.i * R.p
l = E.f * R.b + E.g * R.g + E.h * R.l + E.i * R.q
u = E.f * R.c + E.g * R.h + E.h * R.m + E.i * R.r
c = E.f * R.d + E.g * R.i + E.h * R.n + E.i * R.s
m = E.k * R.a + E.l * R.f + E.m * R.k + E.n * R.p
h = E.k * R.b + E.l * R.g + E.m * R.l + E.n * R.q
f = E.k * R.c + E.l * R.h + E.m * R.m + E.n * R.r
b = E.k * R.d + E.l * R.i + E.m * R.n + E.n * R.s
y = E.p * R.a + E.q * R.f + E.r * R.k + E.s * R.p
T = E.p * R.b + E.q * R.g + E.r * R.l + E.s * R.q
w = E.p * R.c + E.q * R.h + E.r * R.m + E.s * R.r
k = E.p * R.d + E.q * R.i + E.r * R.n + E.s * R.s
s = E.a * R.e + E.b * R.j + E.c * R.o + E.d * R.t + E.e
d = E.f * R.e + E.g * R.j + E.h * R.o + E.i * R.t + E.j
_ = E.k * R.e + E.l * R.j + E.m * R.o + E.n * R.t + E.o
F = E.p * R.e + E.q * R.j + E.r * R.o + E.s * R.t + E.t
self.a = t
self.b = i
self.c = o
self.d = n
self.e = s
self.f = a
self.g = l
self.h = u
self.i = c
self.j = d
self.k = m
self.l = h
self.m = f
self.n = b
self.o = _
self.p = y
self.q = T
self.r = w
self.s = k
self.t = F
return self
def clone(self):
newMatrix = ColorMatrix()
newMatrix.setMatrix(
self.a,
self.b,
self.c,
self.d,
self.e,
self.f,
self.g,
self.h,
self.i,
self.j,
self.k,
self.l,
self.m,
self.n,
self.o,
self.p,
self.q,
self.r,
self.s,
self.t
)
return newMatrix
def equals(self, other):
return np.array_equal(self.matrix, other.matrix)
def get_offsets(self):
return [self.e, self.j, self.o, self.t]
def __str__(self):
return str(self.matrix)
def to_array(self):
components = [
self.a,
self.b,
self.c,
self.d,
self.e,
self.f,
self.g,
self.h,
self.i,
self.j,
self.k,
self.l,
self.m,
self.n,
self.o,
self.p,
self.q,
self.r,
self.s,
self.t,
]
return components
@staticmethod
def create_brightness_matrix(value):
matrix = ColorMatrix()
matrix.e = value
matrix.j = value
matrix.o = value
return matrix
@staticmethod
def create_contrast_matrix(value):
matrix = ColorMatrix()
i = (1 - value) / 2
matrix.a = matrix.g = matrix.m = value
matrix.e = matrix.j = matrix.o = i
return matrix
@staticmethod
def create_saturation_matrix(value=1):
matrix = ColorMatrix()
i = 1 - value
o = 0.213 * i
n = 0.715 * i
s = 0.072 * i
matrix.a = o + value
matrix.b = n
matrix.c = s
matrix.f = o
matrix.g = n + value
matrix.h = s
matrix.k = o
matrix.l = n
matrix.m = s + value
return matrix
@staticmethod
def create_exposure_matrix(value=0):
exposure = 2**value
return ColorMatrix(exposure, 0, 0, 0, 0, 0, exposure, 0, 0, 0, 0, 0, exposure, 0, 0, 0, 0, 0, 1, 0)
@staticmethod
def createLinearMatrix(value=1, offset=0):
matrix = ColorMatrix()
matrix.a = matrix.g = matrix.m = value
matrix.e = matrix.j = matrix.o = offset
return matrix
# Define the Color class if not already defined
class Color:
def __init__(self, r, g, b, a):
self.r = r
self.g = g
self.b = b
self.a = a