Add twitch support #6
1 changed files with 21 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
from hashlib import md5
|
||||
import re
|
||||
|
||||
import pygame
|
||||
from pygame.locals import Rect
|
||||
|
@ -8,6 +9,20 @@ from chats import AUTHOR_TYPES
|
|||
from . import BG_COLOR, XTERM_COLORS
|
||||
from .TextFragment import TextFragment
|
||||
|
||||
hex_color_regex = re.compile('^#[a-f0-9]{6}$')
|
||||
|
||||
def closest_xterm(target_color):
|
||||
r, g, b = (int(target_color[i:i+2], 16) for i in range(1, len(target_color), 2))
|
||||
|
||||
distances = []
|
||||
for xterm_color in XTERM_COLORS:
|
||||
xr, xg, xb = xterm_color
|
||||
distance = math.sqrt(abs(r - xr) ** 2 + abs(g - xg) ** 2 + abs(b - xb) ** 2)
|
||||
if distance == 0:
|
||||
return xterm_color
|
||||
distances.append((distance, xterm_color))
|
||||
return min(distances)[1]
|
||||
|
||||
|
||||
class MessageView:
|
||||
# TODO: Superchat and chat sponsor handling
|
||||
|
@ -26,6 +41,10 @@ class MessageView:
|
|||
author_id = message['author_id']
|
||||
author_type = message['author_type']
|
||||
|
||||
author_color = message.get('author_color')
|
||||
if author_color is not None and hex_color_regex.match(author_color):
|
||||
author_color = closest_xterm(author_color)
|
||||
else:
|
||||
author_hash = int(md5(author_id.encode('utf-8')).hexdigest(), 16)
|
||||
author_color = (author_hash % 144) + 88
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue