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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
from pytest import approx as assert_list_almost_equal
from colormap import colors
from colormap.colors import *
def test_hex2web():
assert hex2web("#FFAA11") == "#FA1"
def test_web2hex():
assert web2hex("#FA1") == "#FFAA11"
def test_rgb2yuv():
assert_list_almost_equal(rgb2yuv(1, 1, 1), (1, 0, 0))
assert_list_almost_equal(rgb2yuv_int(255, 255, 255), (255, 0, 0))
assert_list_almost_equal(yuv2rgb(1, 0, 0), (1, 1, 1))
assert_list_almost_equal(yuv2rgb_int(255, 0, 0), (255, 255, 255))
def test_rgb2hsv():
assert_list_almost_equal(colors.rgb2hsv(0, 1, 1), (0.5, 1, 1))
assert_list_almost_equal(colors.rgb2hsv(0, 255, 255, normalised=False), (0.5, 1, 1))
assert_list_almost_equal(hsv2rgb(0.5, 1, 1), (0, 1, 1))
assert_list_almost_equal(colors.hsv2rgb(180, 100, 100, normalised=False), (0, 1, 1))
def test_rgb2hls():
assert_list_almost_equal(colors.rgb2hls(0, 1, 1), (0.5, 0.5, 1))
assert_list_almost_equal(colors.rgb2hls(0, 255, 255, normalised=False), (0.5, 0.5, 1))
assert_list_almost_equal(colors.hls2rgb(0.5, 0.5, 1), (0, 1, 1))
assert_list_almost_equal(colors.hls2rgb(180, 50, 100, normalised=False), (0.0, 1, 1))
def test_hex2dec():
assert colors.hex2dec("FF") == 1
assert colors.hex2dec("#FF") == 1
def test_rgb2hex():
colors.rgb2hex(0, 0, 255)
colors.rgb2hex(0, 0, 1)
colors.rgb2hex(*(0, 0, 1))
try:
colors.rgb2hex([0, 0])
assert False
except:
assert True
try:
colors.rgb2hex(0, 0, 1000)
assert False
except:
assert True
try:
colors.rgb2hex(0, 0, -1000)
assert False
except:
assert True
try:
colors.rgb2hex(0, 0, 10, normalised=True)
assert False
except:
assert True
def testColors():
# test constructors
c = colors.Color("#FFF")
c = colors.Color(rgb=(0, 0, 0))
c = colors.Color(hls=(0, 0, 0))
c = colors.Color(hsv=(0, 0, 0))
c = colors.Color(c)
try:
colors.Color()
assert False
except:
assert True
try:
colors.Color(object)
assert False
except:
assert True
# test setter/getter
c = colors.Color("Blue")
assert c.rgb == (0, 0, 1)
assert c.hex == "#0000FF"
assert_list_almost_equal(c.hsv, (0.66666666666666, 1, 1))
assert c.value == 1
assert_list_almost_equal(c.hls, (0.666666666666666, 0.5, 1))
print(c)
c.normalised = True
c.name
c.hsv
c.hls
print(c)
assert c.rgb == (0, 0, 1)
c.rgb = (0, 0, 1)
c.hsv = (0, 0, 1)
c.normalised = False
# name can be changed and affects RGB/HEX
c.name = "Magenta"
assert c.rgb == colors._normalise(255, 0.0, 255)
assert c.hex == "#FF00FF"
# hex can be changed and affects name/HEX
c.hex = "#F8F8FF"
assert c.name == "Ghost White" # non official name
# assert c.rgb ==
# RGB can be changed and affects name/HEX
c.rgb = colors._normalise(248, 248, 255)
assert c.name == "Ghost White" # official name
assert c.hex == "#F8F8FF"
assert c.name == "Ghost White" # non official but works
assert c.hex == "#F8F8FF"
c.saturation_hls = 0.5
assert c.saturation_hls == 0.5
c.lightness = 0.5
assert c.lightness == 0.5
c.hue = 0.5
assert c.hue == 0.5
c.hex = "#FF1F1F"
assert c.name == "undefined"
try:
c.hex = "ZFF1F1F"
assert False
except:
assert True
c = colors.Color("red")
assert c.red == 1
assert c.green == 0
assert c.blue == 0
c.blue = 0.0
c.green = 0.0
c.red = 0
assert c.name == "Black"
c.value
c.value = 0.5
c.yiq
def test_normalise():
colors._normalise(255, 255, 255, mode="rgb") == (1, 1, 1)
colors._normalise(*(255, 255, 255), mode="rgb") == (1, 1, 1)
colors._normalise(*(360, 100, 100), mode="hls") == (1, 1, 1)
colors._denormalise(*(1, 1, 1), mode="rgb") == (255, 255, 255)
colors._denormalise(*(1, 1, 1), mode="hls") == (360, 100, 100)
def test_to_intensity():
to_intensity(0.5)
def test_plot_colormap():
plot_colormap("jet")
plot_colormap("jet_r")
try:
plot_colormap("dummy")
assert False
except (AssertionError, TypeError):
assert True
def test_plot_category():
plot_category("diverging")
def test_colormap():
try:
from pylab import clf, close, gcf
except:
return
c = Colormap()
cmap = c.get_cmap_heat()
# c.test_cmap(cmap)
f = gcf()
# f.close()
# design your own colormap
d = {"blue": [0, 0, 0, 1, 1, 1, 0], "green": [0, 1, 1, 1, 0, 0, 0], "red": [1, 1, 0, 0, 0, 1, 1]}
cmap = c.cmap(d, reverse=True)
cmap = c.cmap("jet", reverse=True)
cmap = c.cmap("heat")
cmap = c.cmap("heat_r")
cmap = c.get_cmap_rainbow()
cmap = c.get_cmap_red_green()
cmap = c.get_cmap_heat_r()
t = [
"#FF0000FF",
"#FF4D00FF",
"#FF9900FF",
"#FFE500FF",
"#CCFF00FF",
"#80FF00FF",
"#33FF00FF",
"#00FF19FF",
"#00FF66FF",
"#00FFB2FF",
"#00FFFFFF",
"#00B3FFFF",
"#0066FFFF",
"#001AFFFF",
"#3300FFFF",
"#7F00FFFF",
"#CC00FFFF",
"#FF00E6FF",
"#FF0099FF",
"#FF004DFF",
]
# FIXME: need to find a way to close the plot. close('all') does not work
# c.plot_rgb_from_hex_list(t)
c.plot_colormap("misc")
try:
c.plot_colormap("dummy")
assert False
except ValueError:
pass
c.test_colormap("jet")
c.diverging
c.colormaps
c.sequentials
c.sequentials2
c.qualitative
c.cyclic
c.perceptually_uniform
t = [
"#FF0000FF",
"#FF4D00FF",
"#FF9900FF",
"#FFE500FF",
"#CCFF00FF",
"#80FF00FF",
"#33FF00FF",
"#00FF19FF",
"#00FF66FF",
"#00FFB2FF",
"#00FFFFFF",
"#00B3FFFF",
"#0066FFFF",
"#001AFFFF",
"#3300FFFF",
"#7F00FFFF",
"#CC00FFFF",
"#FF00E6FF",
"#FF0099FF",
"#FF004DFF",
]
c.plot_rgb_from_hex_list(t)
c.test_colormap() # no input plots the heat map
def test_HEX():
h = HEX()
h.get_standard_hex_color("0xFFF")
try:
h.get_standard_hex_color(22)
assert False
except:
assert True
try:
h.get_standard_hex_color("r")
assert False
except:
assert True
try:
h.get_standard_hex_color("rrrrrrrrrrrr")
assert False
except:
assert True
try:
h.get_standard_hex_color("#AAAZZZ")
assert False
except:
assert True
try:
h.get_standard_hex_color("#AAAA")
assert False
except:
assert True
|