Spaces:
Runtime error
Runtime error
File size: 3,763 Bytes
94ff611 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import math
from color import Color
class ColorWheel:
@property
def baseColor(self):
return self._baseColor
@property
def hue(self):
return [
self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12), self.addH(3.0 / 12), self.addH(4.0 / 12), self.addH(5.0 / 12),
self.addH(-6.0 / 12), self.addH(-5.0 / 12), self.addH(-4.0 / 12), self.addH(-3.0 / 12), self.addH(-2.0 / 12), self.addH(-1.0 / 12)
]
@property
def tone(self):
return [self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16)]
@property
def tone15(self):
return [self.addWhite(-7.0 / 16), self.addWhite(-6.0 / 16), self.addWhite(-5.0 / 16), self.addWhite(-4.0 / 16), self.addWhite(-3.0 / 16),
self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16),
self.addWhite(3.0 / 16), self.addWhite(4.0 / 16), self.addWhite(5.0 / 16), self.addWhite(6.0 / 16), self.addWhite(7.0 / 16)]
@property
def complementaryColors(self):
return [self.baseColor, self.addH(0.5)]
@property
def triadicColors(self):
return [self.addH(-4.0 / 12), self.baseColor, self.addH(4.0 / 12)]
@property
def splitComplementaryColors(self):
return [self.addH(-5.0 / 12), self.baseColor, self.addH(5.0 / 12)]
@property
def analogousColors(self):
return [self.addH(-2.0 / 12), self.addH(-1.0 / 12), self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12)]
def __init__(self, c):
self._baseColor = c
self._r = c.r / 255.0
self._g = c.g / 255.0
self._b = c.b / 255.0
@staticmethod
def fromHsv(h, s, v):
r, g, b = ColorWheel.hsvToRgb(h, s, v)
c = Color.fromRgb(round(r * 255), round(g * 255), round(b * 255))
return ColorWheel(c)
def addWhite(self, value):
r, g, b = (self._r + value, self._g + value, self._b + value)
r = min(max(r, 0.0), 1.0)
g = min(max(g, 0.0), 1.0)
b = min(max(b, 0.0), 1.0)
return self._fromRgb(r, g, b)
def addH(self, value):
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
h = (h + value) % 1.0
if h < 0.0:
h += 1.0
r, g, b = ColorWheel.hsvToRgb(h, s, v)
return self._fromRgb(r, g, b)
def addS(self, value):
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
s += value
s = min(max(s, 0.0), 1.0)
r, g, b = ColorWheel.hsvToRgb(h, s, v)
return self._fromRgb(r, g, b)
def addV(self, value):
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
v += value
v = min(max(v, 0.0), 1.0)
r, g, b = ColorWheel.hsvToRgb(h, s, v)
return self._fromRgb(r, g, b)
@staticmethod
def rgbToHsv(r, g, b):
if r < 0.0 or r > 1.0:
raise ValueError()
if g < 0.0 or g > 1.0:
raise ValueError()
if b < 0.0 or b > 1.0:
raise ValueError()
cmax = max(r, g, b)
cmin = min(r, g, b)
h = cmax - cmin
if h > 0.0:
if cmax == r:
h = (g - b) / h
if h < 0.0:
h += 6.0
elif cmax == g:
h = 2.0 + (b - r) / h
else:
h = 4.0 + (r - g) / h
h /= 6.0
s = cmax - cmin
if cmax != 0.0:
s /= cmax
v = cmax
return h, s, v
@staticmethod
def hsvToRgb(h, s, v):
if h < 0.0 or h > 1.0:
raise ValueError()
if s < 0.0 or s > 1.0:
raise ValueError()
if v < 0.0 or v > 1.0:
raise ValueError()
r = v
g = v
b = v
if s > 0.0:
h *= 6.0
i = math.floor(h)
f = h - i
if i == 1:
r *= 1 - s * f
b *= 1 - s
elif i == 2:
r *= 1 - s
b *= 1 - s * (1 - f)
elif i == 3:
r *= 1 - s
g *= 1 - s * f
elif i == 4:
r *= 1 - s * (1 - f)
g *= 1 - s
elif i == 5:
g *= 1 - s
b *= 1 - s * f
else:
g *= 1 - s * (1 - f)
b *= 1 - s
return r, g, b
def _fromRgb(self, r, g, b):
return Color.fromArgb(self.baseColor.a, round(r * 255), round(g * 255), round(b * 255))
|