/usr/lib/python3/dist-packages/ansi/colour/rgb.py is in python3-ansi 0.1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from ansi.sequence import sequence
ANSI_COLOURS = (
(0x00, 0x00, 0x00), (0xcd, 0x00, 0x00),
(0x00, 0xcd, 0x00), (0xcd, 0xcd, 0x00),
(0x00, 0x00, 0xee), (0xcd, 0x00, 0xcd),
(0x00, 0xcd, 0xcd), (0xe5, 0xe5, 0xe5),
(0x7f, 0x7f, 0x7f), (0xff, 0x00, 0x00),
(0x00, 0xff, 0x00), (0xff, 0xff, 0x00),
(0x5c, 0x5c, 0xff), (0xff, 0x00, 0xff),
(0x00, 0xff, 0xff), (0xff, 0xff, 0xff),
)
def rgb_distance(rgb1, rgb2):
'''
Calculate the distance between two RGB sequences.
'''
return sum(map(lambda c: (c[0] - c[1]) ** 2,
zip(rgb1, rgb2)))
def rgb_reduce(r, g, b, mode=8):
'''
Convert an RGB colour to 8 or 16 colour ANSI graphics.
'''
colours = ANSI_COLOURS[:mode]
matches = [(rgb_distance(c, map(int, [r, g, b])), i)
for i, c in enumerate(colours)]
matches.sort()
return sequence('m')(str(30 + matches[0][1]))
def rgb8(r, g, b):
'''
Convert an RGB colour to 8 colour ANSI graphics.
'''
return rgb_reduce(r, g, b, 8)
def rgb16(r, g, b):
'''
Convert an RGB colour to 16 colour ANSI graphics.
'''
return rgb_reduce(r, g, b, 16)
def rgb256(r, g, b):
'''
Convert an RGB colour to 256 colour ANSI graphics.
'''
grey = False
poss = True
step = 2.5
while poss: # As long as the colour could be grey scale
if r < step or g < step or b < step:
grey = r < step and g < step and b < step
poss = False
step += 42.5
if grey:
colour = 232 + int(float(sum([r, g, b]) / 33.0))
else:
colour = sum([16] + [int (6 * float(val) / 256) * mod
for val, mod in ((r, 36), (g, 6), (b, 1))])
return sequence('m', fields=3)(38, 5, colour)
|