-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcolors.py
More file actions
1155 lines (965 loc) · 26.5 KB
/
colors.py
File metadata and controls
1155 lines (965 loc) · 26.5 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Color manipulation numerics module
-----------------------------------
| Copyright 2022 by Joel C. Alcarez |
| [[email protected]] |
|-----------------------------------|
| We make absolutely no warranty |
| of any kind, expressed or implied |
|-----------------------------------|
| Contact primary author |
| if you plan to use this |
| in a commercial product at |
-----------------------------------
"""
from random import randint
from array import array
from typing import Callable
from .conditionaltools import iif
from .bmppal import(
bmpstdpal,
bmpvalidcolorbits
)
from .mathlib import(
addvect,
dist,
intscalarmulvect,
intsetminmaxvec,
lerp,
mean,
mulvect,
ravel2D,
roundvect,
scalarmulvect,
setminmax,
setminmaxvec,
subvect,
)
def getdefaultlumrange() -> dict:
"""Gets the default luminosity
ranges lookup
Returns:
a dict for default
luminosity ranges
"""
return {'maxdesc': [255, 0],
'maxasc': [0, 255],
'middesc': [192, 64],
' midasc': [64, 192],
'upperdesc': [255, 128],
'upperasc': [128, 255],
'lowerdesc': [128, 0],
'lowerasc': [0, 128]}
def isvalidcolorbit(bits: int) -> bool:
"""Checks if bits is in the valid
color bits list (1, 4, 8, 24)
Args:
bits: int value
Returns:
True if bits in (1, 4, 8, 24)
False if other values not in
the list above
"""
return bits in bmpvalidcolorbits
def getdefaultbitpal(
bits: int) -> list:
"""Gets the standard bitmap palette
for a specified bit depth bits
Args:
bits: int value (1, 4, 8, 24)
Returns:
list of palette entries
"""
return bmpstdpal[bits]
def colormix(lum: int,
RGBfactors: list[float, float, float]
) -> int:
"""Mix a byte luminosity value to
an rgb triplet that express
a color value in [r, g, b]
ratios from 0.0 to 1.0 to
obtain an int value for a
specific color
Args:
lum : a byte value for
luminosity
RGBfactors: list[r: float,
g: float,
b: float]
float values from
0.0 to 1.0
Returns:
int color val
"""
(r, g, b) = RGBfactors
return RGB2int(int(r * lum),
int(g * lum),
int(b * lum))
def int2RGB(i: int):
"""Break down int color i to its
byte valued r, g and b
components
Args:
i: int color value
Returns:
r: byte, g: byte, b: byte
"""
return i >> 16, (i >> 8) & 0xff, i & 0xff
def int2RGBlist(i: int
) -> list[int, int, int]:
"""Break down int color i to its
byte valued r, g and b
components in a list
Args:
i: int color value
Returns:
[r: byte, g: byte, b: byte]
"""
return [i >> 16, (i >> 8) & 0xff,
i & 0xff]
def int2BGRarr(i: int) -> array:
"""Returns a bgr array from
int i color input
Args:
i: color value
Returns:
unsigned byte BGR array
"""
return array('B', [i & 0xff,
(i >> 8) & 0xff,
i >> 16])
def int2RGBarr(i: int) -> array:
"""Returns a rgb array from
int i color input
Args:
i: color value
Returns:
unsigned byte RGB array
"""
return array('B', [i >> 16,
(i >> 8) & 0xff,
i & 0xff])
def RGB2BGRarr(r: int,
g: int,
b: int) -> array:
"""Returns a bgr array from
individual r, g and b color
component inputs
Args:
r, g, b: byte color values
Returns:
unsigned byte BGR array
"""
return array('B', [b, g, r])
def RGBfactors2RGB(
RGBfactors: list[float,
float,
float],
bytelum: int
) -> list[int,
int,
int]:
"""Mix a byte luminosity value to
an rgb triplet that express
a color value in [r, g, b]
ratios from 0.0 to 1.0 to
obtain byte r, g, b values
stored in a list [r, g, b]
Args:
lum : a byte value for
luminosity
RGBfactors: list[r: float,
g: float,
b: float]
float values from
0.0 to 1.0
Returns:
[r: byte, g: byte, b: byte]
"""
return roundvect(scalarmulvect(
RGBfactors, bytelum))
def RGB2int(r: int, g: int, b: int
) -> int:
"""Pack byte r, g and b color value
components to an int
representation for a
specific color
Args:
r, g, b: color byte values
Returns:
int color val
"""
return b + (g << 8) + (r << 16)
def matchRGBtodefault4bitpal(
RGB: list[int, int, int]
) -> int:
"""Deterministic color matching
from a 24-bit palette to a
the default 4-bit palette
using formulas
Args:
RGB: color byte values
[r: byte,
g: byte,
b: byte]
Returns:
int color val (4-bit)
"""
(r, g, b) = RGB
r >>= 6
g >>= 6
b >>= 6
color = 8 if r > 1 or g > 1 or b > 1 else 0
if r >= 1:
color += 4
if g >= 1:
color += 2
if b >= 1:
color += 1
return color
def matchRGBtopal(RGB: list,
pal: list) -> int:
"""Color matching from a 24-bit
palette to any 1, 4 or 8-bit
palette using Euclidean
distance minimization
in an rgb colorspace for
the closest color match
Args:
RGB: color byte values
[r: byte,
g: byte,
b: byte]
pal: the bmp palette to match
Returns:
int color val (4-bit)
"""
c = i = 0
d = 442
if RGB in pal:
c = pal.index(RGB)
else:
for p in pal:
if p != [0,0,0] and RGB != [0,0,0]:
newd = dist(RGB, p)
if newd < d:
c = i
d = newd
i += 1
return c
def RGBtoRGBfactorsandlum(
rgb: list[int, int, int]
) -> list[list[float, float,
float], int]:
"""Separates luminosity from
color and express color
as ratios of r, g and b
float values from 0.0 to 1.0
Args:
rgb: color byte values
[r: byte,
b: byte,
g: byte]
Returns:
list[list[r: float,
g: float,
b: float], lum: byte]
"""
lum = max(rgb)
if lum == 0:
lum = 1
return [[rgb[0] / lum,
rgb[1] / lum,
rgb[2] / lum], lum]
def probplotRGBto1bit(
rgb: list[int, int, int],
brightness: int) -> int:
"""Use a non deterministic plot
to convert 24-bit colors to
1-bit
Args:
rgb: color byte values
[r: byte,
g: byte,
b: byte]
Returns:
0 or 1
"""
return round(brightness * randint(0, sum(rgb)) / 768)
def probplotRGBto4bitpal(
rgb: list[int, int, int]
) -> int:
"""Use a non deterministic plot
to convert 24-bit colors to
4-bits
Args:
rgb: color byte values
[r: byte,
g: byte,
b: byte]
Returns:
4 bit int value
"""
color = 0
(r, g, b) = rgb
if round(randint(0, r) / 256) == 1:
color += 4
if round(randint(0, g) / 256) == 1:
color += 2
if round(randint(0, b) / 256) == 1:
color += 1
r >>= 6
g >>= 6
b >>= 6
if r > 1 or g > 1 or b > 1:
color += 8
return color
def monochromepal(
bits: int,
rgbfactors: list[float,
float,
float]
) -> list[list[int,
int,
int]]:
"""Returns a monochrome palette
based on bit depth bits and
rgbfactors
Args:
bits : bit depth
(1, 4, 8)
rgbfactors: color values
0.0 to 1.0
[r: float,
g: float,
b: float]
Returns:
a palette as
list[list[r: int, g: int, b int]]
"""
inc = (256 >> bits) + \
iif(bits == 4, 1,
iif(bits == 1, 127, 0))
(r, g, b) = rgbfactors
return [[round(r * c),
round(g * c),
round(b * c)]
for c in range(0, 256, inc)]
def monoshiftablepal(
bits: int,
rgbfactors: list[float,
float,
float],
mult: int = 1,
shift: int = 0,
) -> list[list[int,
int,
int]]:
"""Returns an adjustable monochrome palette
based on bit depth bits and rgbfactors
Args:
bits : bit depth
(1, 4, 8)
mult : value multiplier
shift : value shift
rgbfactors: color values
0.0 to 1.0
[r: float,
g: float,
b: float]
Returns:
a palette as
list[list[r: int, g: int, b int]]
"""
inc = (256 >> bits) + \
iif(bits == 4, 1,
iif(bits == 1, 127, 0))
(r, g, b) = rgbfactors
return [[round(r * j),
round(g * j),
round(b * j)]
for j in [(c * mult + shift) % 256 for c in range(0, 256, inc)]]
def monoinverseshiftablepal(
bits: int,
rgbfactors: list[float,
float,
float],
mult: int = 1,
shift: int = 0,
) -> list[list[int,
int,
int]]:
"""Returns an adjustable monochrome palette
with reversed brightness
based on bit depth bits and rgbfactors
Args:
bits : bit depth
(1, 4, 8)
mult : value multiplier
shift : value shift
rgbfactors: color values
0.0 to 1.0
[r: float,
g: float,
b: float]
Returns:
a palette as
list[list[r: int, g: int, b: int]]
"""
inc = (256 >> bits) + \
iif(bits == 4, 1,
iif(bits == 1, 127, 0))
(r, g, b) = rgbfactors
return [[round(r * j),
round(g * j),
round(b * j)]
for j in [((255 - c) * mult + shift) % 256 for c in range(0, 256, inc)]]
def monoinverseshiftableBGRpal(
bits: int,
rgbfactors: list[float,
float,
float],
mult: int = 1,
shift: int = 0,
) -> list[list[int,
int,
int]]:
"""Returns an adjustable BGR monochrome palette
with reversed brightness
based on bit depth bits and rgbfactors
Args:
bits : bit depth
(1, 4, 8)
mult : value multiplier
shift : value shift
rgbfactors: color values
0.0 to 1.0
[r: float,
g: float,
b: float]
Returns:
a palette as
list[list[b: int, g: int, r: int]]
"""
inc = (256 >> bits) + \
iif(bits == 4, 1,
iif(bits == 1, 127, 0))
(r, g, b) = rgbfactors
return [[round(b * j),
round(g * j),
round(r * j)]
for j in [((255 - c) * mult + shift) % 256 for c in range(0, 256, inc)]]
def monoinverseshiftableBGRApal(
bits: int,
rgbfactors: list[float,
float,
float],
mult: int = 1,
shift: int = 0,
) -> list[list[int,
int,
int]]:
"""Returns an adjustable BGRA monochrome palette
with reversed brightness
based on bit depth bits and rgbfactors
Args:
bits : bit depth
(1, 4, 8)
mult : value multiplier
shift : value shift
rgbfactors: color values
0.0 to 1.0
[r: float,
g: float,
b: float]
Returns:
a palette as
list[list[b: int, g: int, r: int, a: int = 0]]
"""
inc = (256 >> bits) + \
iif(bits == 4, 1,
iif(bits == 1, 127, 0))
(r, g, b) = rgbfactors
return [[round(b * j),
round(g * j),
round(r * j),
0]
for j in [((255 - c) * mult + shift) % 256 for c in range(0, 256, inc)]]
def monochrome(rgb: list[int, int, int]
) -> list[int, int, int]:
"""Returns a monochrome color
based on a 24-bit RGB value
Args:
rgb: color values
[r: byte, g: byte, b: byte]
Returns:
a gray color (r = g = b)
[r: byte, g: byte, b: byte]
"""
return [round(mean(rgb))] * 3
def gammacorrectbyte(lumbyte: int,
gamma: float
) -> int:
"""Apply a gamma factor to a
luminosity byte value
Args:
lumbyte: byte luminosity
value
gamma : gamma adjustment
Returns:
a gamma adjusted byte value
"""
return int(((lumbyte / 255) ** gamma) * 255)
def gammacorrect(
rgb: list[int, int, int],
gamma: float
) -> list[int, int, int]:
"""Apply a gamma factor to a rgb
Args:
rgb: color as [r: byte,
g: byte,
b: byte]
gamma : gamma adjustment
Returns:
a gamma adjusted color as
[r: byte, g: byte, b: byte]
"""
c = RGBtoRGBfactorsandlum(rgb)
return setminmaxvec(RGBfactors2RGB(c[0],
gammacorrectbyte(c[1], gamma)),
0, 255)
def brightnessadjust(
rgb: list[int, int, int],
percentadj: float
) -> list[int, int, int]:
"""Apply a brightness adjustment
to a rgb
Args:
rgb: color as [r: byte,
g: byte,
b: byte]
percentadj: brightness
adjustment
in percent
can be positive
or negative
Returns:
a brightness adjusted color as
[r: byte, g: byte, b: byte]
"""
(c0, c1) = RGBtoRGBfactorsandlum(rgb)
return setminmaxvec(RGBfactors2RGB(c0,
c1 * (1 + (percentadj / 100))),
0, 255)
def thresholdadjust(
rgb: list[int, int, int],
lumrange: list[int, int]
) -> list[int, int, int]:
"""Apply a threshold adjustment
to a rgb
Args:
rgb: color as [r: byte,
g: byte,
b: byte]
lumrange: [min: byte,
max: byte]
brightness
threshold
adjustment
limits
Returns:
a brightness threshold adjusted
color as [r: byte,
g: byte,
b: byte]
"""
(c0, c1) = RGBtoRGBfactorsandlum(rgb)
(l0, l1) = intsetminmaxvec(lumrange, 0, 255)
if l0 > l1:
l1, l0 = l0, l1
return RGBfactors2RGB(c0,
setminmax(c1, l0, l1))
def colorfilter(
rgb: list[int, int, int],
rgbfactors: list[float,
float,
float]
) -> list[int, int, int]:
"""Apply a color filter
rgbfactors to rgb
Args:
rgb: color as [r: byte,
g: byte,
b: byte]
rgbfactors: color filter as
[r: float,
g: float,
b: float]
Returns:
a color filtered
color as [r: byte,
g: byte,
b: byte]
"""
return intsetminmaxvec(mulvect(
rgb, rgbfactors), 0, 255)
def applymonochromefiltertoBGRbuf(
buf: array):
"""Apply a monochrome filter to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
rgbfactors: color filter as
[r: float,
g: float,
b: float]
Returns:
byref unsigned byte array
holding mono BGR data
"""
m = len(buf)
buf[0 : m - 2 : 3] = \
buf[1 : m - 1 : 3] = \
buf[2 : m : 3] = array(
'B',
[
int((b + g + r) / 3)
for b, g, r in zip(
buf[ : m - 2 : 3],
buf[1 : m - 1 : 3],
buf[2 : m : 3]
)
]
)
def monochromefiltertoBGRbuf(
buf: array) -> array:
"""Apply a monochrome filter to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
rgbfactors: color filter as
[r: float,
g: float,
b: float]
Returns:
unsigned byte array
holding mono BGR data
"""
applymonochromefiltertoBGRbuf(buf)
return buf
def applycolorfiltertoBGRbuf(
buf: array,
rgbfactors: list[float,
float, float]):
"""Apply a color filter to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
rgbfactors: color filter as
[r: float,
g: float,
b: float]
Returns:
byref unsigned byte array
holding color BGR data
"""
m = len(buf) - 1
(r, g, b) = rgbfactors
buf[0 : m - 2 : 3], buf[1 : m - 1 : 3], buf[2: m: 3] = (
array('B', intscalarmulvect(buf[ : m - 2 : 3], b)),
array('B', intscalarmulvect(buf[1 : m - 1 : 3], g)),
array('B', intscalarmulvect(buf[2 : m : 3], r)),
)
def colorfiltertoBGRbuf(
buf: array,
rgbfactors: list[float,
float, float]
) -> array:
"""Apply a color filter to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
rgbfactors: color filter as
[r: float,
g: float,
b: float]
Returns:
unsigned byte array
holding color BGR data
"""
applycolorfiltertoBGRbuf(buf,rgbfactors)
return buf
def applygammaBGRbuf(
buf: array, gamma: float):
"""Apply a gamma adjustment to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
gamma: float gamma adjust
Returns:
byref unsigned byte array
holding gamma adjusted
BGR data
"""
imax = len(buf)
for i in range(0, imax, 3):
lum = max(buf[i: i + 3])
if lum == 0:
lum = 1
f = int(((lum / 255) ** gamma) * 255) / \
lum
j = i + 1
k = i + 2
buf[i] = int(buf[i] * f) & 0xff
buf[j] = int(buf[j] * f) & 0xff
buf[k] = int(buf[k] * f) & 0xff
def gammaBGRbuf(
buf: array,
gamma: float) -> array:
"""Apply a gamma adjustment to a
BGR buffer
Args:
buf: unsigned byte array
holding BGR data
gamma: float gamma adjust
Returns:
unsigned byte array
holding gamma adjusted
BGR data
"""
applygammaBGRbuf(buf, gamma)
return buf
def gammacorrectbyte(lumbyte: int,
gamma: float) -> int:
"""Apply a gamma adjustment
to a byte
Args:
lumbyte: int value
gamma: float gamma adjust
Returns:
unsigned gamma adjusted byte
"""
return int(((lumbyte / 255) ** gamma) * 255)
def RGBfactorstoBaseandRange(
lumrange: list[int, int],
rgbfactors: list[float,
float,
float]):
"""Get base color luminosity and
luminosity range from color
expressed as r, g, b float
values and min and max byte
luminosity values
Args:
lumrange: [minval: byte
maxval: byte]
rgbfactors: color as
[r: float,
g: float,
b: float]
Returns:
base luminosity as
[r: byte, g: byte, b: byte]
luminosity range as
[r: byte, g: byte, b: byte]
"""
baselum = intscalarmulvect(
rgbfactors,
lumrange[0])
lumrange = subvect(scalarmulvect(
rgbfactors,
lumrange[1]),
baselum)
return baselum, lumrange
def invertbitsinbuffer(buf: array
) -> array:
"""Flips all the bits in an
unsigned byte array
Args:
buf: unsigned byte array
Returns:
bit flipped unsigned byte array
"""
return array('B', [b ^ 0xFF
for b in buf])
def applybrightnessadjtoBGRbuf(
buf: array,
percentadj: float) -> array:
"""Apply a brightness adjustment
to a BGR buffer
Args:
buf: unsigned byte array
holding BGR data
percentadj: float brightness
adjust can be
positive or
negative
Returns:
unsigned byte array
holding brightness adjusted
BGR data
"""
return array('B',setminmaxvec(
intscalarmulvect(buf, 1 + (percentadj / 100)), 0, 255))
def applythresholdadjtoBGRbuf(
buf: array,
lumrange: list) -> array:
"""Apply a threshold adjustment
to a BGR buffer
Args:
buf: unsigned byte array
holding BGR data
lumrange: [min: int, max: int]
brightness threshold
Returns:
unsigned byte array
holding threshold adjusted
BGR data
"""
lummin = lumrange[0] & 0xff
lummax = lumrange[1] & 0xff
m = len(buf)
for i in range(0, m, 3):
lum = max(buf[i: i + 3])
f = 1
if lummin > lummax:
lummin, lummax = lummax, lummin
if lum > 0:
if lum < lummin:
f = lummin / lum