This commit is contained in:
Emmanuel Gil Peyrot 2017-11-14 12:13:03 +00:00
parent 395435c68d
commit bd7b52988c
7 changed files with 34 additions and 15 deletions

View file

@ -5,14 +5,15 @@ import math
# BT.601 (YCbCr) constants, see XEP-0392
K_R = 0.299
K_G = 0.587
K_B = 1-K_R-K_G
K_B = 1 - K_R - K_G
def ncurses_color_to_rgb(color):
if color <= 15:
try:
(r, g, b) = curses.color_content(color)
except: # fallback in faulty terminals (e.g. xterm)
(r, g, b) = curses.color_content(color%8)
(r, g, b) = curses.color_content(color % 8)
r = r / 1000 * 5
g = g / 1000 * 5
b = b / 1000 * 5
@ -28,12 +29,14 @@ def ncurses_color_to_rgb(color):
r = g = b = color / 24 * 5
return r / 5, g / 5, b / 5
def rgb_to_ycbcr(r, g, b):
y = K_R * r + K_G * g + K_B * b
cr = (r - y) / (1 - K_R) / 2
cb = (b - y) / (1 - K_B) / 2
return y, cb, cr
def generate_ccg_palette(curses_palette, reference_y):
cbcr_palette = {}
for curses_color in curses_palette:
@ -56,12 +59,14 @@ def generate_ccg_palette(curses_palette, reference_y):
for angle, (_, curses_color) in cbcr_palette.items()
}
def text_to_angle(text):
hf = hashlib.sha1()
hf.update(text.encode("utf-8"))
hue = int.from_bytes(hf.digest()[:2], "little")
return hue / 65535 * math.pi * 2
def angle_to_cbcr_edge(angle):
cr = math.sin(angle)
cb = math.cos(angle)
@ -69,14 +74,16 @@ def angle_to_cbcr_edge(angle):
factor = 0.5 / abs(cr)
else:
factor = 0.5 / abs(cb)
return cb*factor, cr*factor
return cb * factor, cr * factor
def cbcr_to_angle(cb, cr):
magn = math.sqrt(cb**2 + cr**2)
if magn > 0:
cr /= magn
cb /= magn
return math.atan2(cr, cb) % (2*math.pi)
return math.atan2(cr, cb) % (2 * math.pi)
def ccg_palette_lookup(palette, angle):
# try quick lookup first
@ -97,6 +104,7 @@ def ccg_palette_lookup(palette, angle):
return best
def ccg_text_to_color(palette, text):
angle = text_to_angle(text)
return ccg_palette_lookup(palette, angle)

View file

@ -1076,8 +1076,9 @@ class MucTab(ChatTab):
def enable_self_ping_event(self):
delay = config.get_by_tabname(
"self_ping_delay", self.general_jid, default=0)
interval = int(config.get_by_tabname(
"self_ping_interval", self.general_jid, default=delay))
interval = int(
config.get_by_tabname(
"self_ping_interval", self.general_jid, default=delay))
if interval <= 0: # use 0 or some negative value to disable it
return
self.disable_self_ping_event()
@ -1091,7 +1092,8 @@ class MucTab(ChatTab):
self.self_ping_event = None
def send_self_ping(self):
timeout = config.get_by_tabname("self_ping_timeout", self.general_jid, default=60)
timeout = config.get_by_tabname(
"self_ping_timeout", self.general_jid, default=60)
to = self.name + "/" + self.own_nick
self.core.xmpp.plugin['xep_0199'].send_ping(
jid=to,

View file

@ -373,6 +373,7 @@ class Theme(object):
prepare_ccolor_palette(self)
return self.CCG_PALETTE
# This is the default theme object, used if no theme is defined in the conf
theme = Theme()
@ -467,7 +468,8 @@ def to_curses_attr(color_tuple):
if 'u' in additional_val:
curses_pair = curses_pair | curses.A_UNDERLINE
if 'i' in additional_val:
curses_pair = curses_pair | (curses.A_ITALIC if hasattr(curses, 'A_ITALIC') else curses.A_REVERSE)
curses_pair = curses_pair | (curses.A_ITALIC if hasattr(
curses, 'A_ITALIC') else curses.A_REVERSE)
if 'a' in additional_val:
curses_pair = curses_pair | curses.A_BLINK
return curses_pair
@ -517,6 +519,7 @@ def update_themes_dir(option=None, value=None):
log.debug('Theme load path: %s', load_path)
def prepare_ccolor_palette(theme):
"""
Prepare the Consistent Color Generation (XEP-0392) palette for a theme.
@ -538,6 +541,7 @@ def prepare_ccolor_palette(theme):
theme.CCG_Y,
)
def reload_theme():
theme_name = config.get('theme')
global theme

View file

@ -56,13 +56,13 @@ class User(object):
if theme.ccg_palette:
# use XEP-0392 CCG
fg_color = colors.ccg_text_to_color(
theme.ccg_palette,
self.jid.bare if self.jid and self.jid.bare else self.nick
)
theme.ccg_palette, self.jid.bare
if self.jid and self.jid.bare else self.nick)
self.color = fg_color, -1
else:
mod = len(theme.LIST_COLOR_NICKNAMES)
nick_pos = int(md5(self.nick.encode('utf-8')).hexdigest(), 16) % mod
nick_pos = int(md5(self.nick.encode('utf-8')).hexdigest(),
16) % mod
self.color = theme.LIST_COLOR_NICKNAMES[nick_pos]
def update(self, affiliation, show, status, role):

View file

@ -101,7 +101,8 @@ class Win(object):
if y is not None and x is not None:
self.move(y, x)
next_attr_char = text.find(FORMAT_CHAR)
attr_italic = curses.A_ITALIC if hasattr(curses, 'A_ITALIC') else curses.A_REVERSE
attr_italic = curses.A_ITALIC if hasattr(
curses, 'A_ITALIC') else curses.A_REVERSE
while next_attr_char != -1 and text:
if next_attr_char + 1 < len(text):
attr_char = text[next_attr_char + 1].lower()

View file

@ -485,7 +485,8 @@ class Input(Win):
if y is not None and x is not None:
self.move(y, x)
format_char = find_first_format_char(text, chars)
attr_italic = curses.A_ITALIC if hasattr(curses, 'A_ITALIC') else curses.A_REVERSE
attr_italic = curses.A_ITALIC if hasattr(
curses, 'A_ITALIC') else curses.A_REVERSE
while format_char != -1:
if text[format_char] == '\n':
attr_char = '|'

View file

@ -213,9 +213,11 @@ def get_body_from_message_stanza(message,
content = content if content else message['body']
return content or " "
def rgb_to_html(rgb):
r, g, b = rgb
return '#%02X%02X%02X' % (round(r*255), round(g*255), round(b*255))
return '#%02X%02X%02X' % (round(r * 255), round(g * 255), round(b * 255))
def ncurses_color_to_html(color):
"""
@ -225,6 +227,7 @@ def ncurses_color_to_html(color):
"""
return rgb_to_html(ncurses_color_to_rgb(color))
def _parse_css_color(name):
if name[0] == '#':
name = name[1:]