From 4e950814dec9ef11fa72dd42ecc9bb22305f101d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 10 Nov 2014 23:33:03 +0100 Subject: [PATCH] Separate hue color of scheme by golden angle issue4257 review12631002 --- CHANGELOG | 1 + tryton/common/common.py | 32 ++++++++----------- .../window/view_form/view/graph_gtk/bar.py | 4 +-- .../window/view_form/view/graph_gtk/graph.py | 2 +- .../window/view_form/view/graph_gtk/line.py | 5 +-- .../window/view_form/view/graph_gtk/pie.py | 4 +-- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8c5268a8d7..2e00d4e58b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* New color scheme for graph * Replace img_{width,height} by width and height attributes Version 3.4.0 - 2014-10-20 diff --git a/tryton/common/common.py b/tryton/common/common.py index cac2ff56b8..f0fcc341b3 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -11,6 +11,7 @@ import subprocess import re import logging import unicodedata +import colorsys from functools import partial from tryton.config import CONFIG from tryton.config import TRYTON_ICON, PIXMAPS_DIR @@ -1177,33 +1178,26 @@ def hex2rgb(hexstring, digits=2): return r / top, g / top, b / top -def clamp(minValue, maxValue, value): - """Make sure value is between minValue and maxValue""" - if value < minValue: - return minValue - if value > maxValue: - return maxValue - return value +def highlight_rgb(r, g, b, amount=0.1): + h, s, v = colorsys.rgb_to_hsv(r, g, b) + return colorsys.hsv_to_rgb(h, s, (v + amount) % 1) -def lighten(r, g, b, amount): - """Return a lighter version of the color (r, g, b)""" - return (clamp(0.0, 1.0, r + amount), - clamp(0.0, 1.0, g + amount), - clamp(0.0, 1.0, b + amount)) - - -def generateColorscheme(masterColor, keys, light=0.06): +def generateColorscheme(masterColor, keys, light=0.1): """ Generates a dictionary where the keys match the keys argument and the values are colors derivated from the masterColor. - Each color is a lighter version of masterColor separated by a difference - given by the light argument. + Each color has a value higher then the previous of `light`. + Each color has a hue separated from the previous by the golden angle. The masterColor is given in a hex string format. """ r, g, b = hex2rgb(COLOR_SCHEMES.get(masterColor, masterColor)) - return dict([(key, lighten(r, g, b, light * i)) - for i, key in enumerate(keys)]) + h, s, v = colorsys.rgb_to_hsv(r, g, b) + if keys: + light = min(light, (1. - v) / len(keys)) + golden_angle = 0.618033988749895 + return {key: colorsys.hsv_to_rgb((h + golden_angle * i) % 1, + s, (v + light * i) % 1) for i, key in enumerate(keys)} class DBProgress(object): diff --git a/tryton/gui/window/view_form/view/graph_gtk/bar.py b/tryton/gui/window/view_form/view/graph_gtk/bar.py index 0d99af6557..e7d391139d 100644 --- a/tryton/gui/window/view_form/view/graph_gtk/bar.py +++ b/tryton/gui/window/view_form/view/graph_gtk/bar.py @@ -3,7 +3,7 @@ #This code is inspired by the pycha project #(http://www.lorenzogil.com/projects/pycha/) from graph import Graph -from tryton.common import float_time_to_text +from tryton.common import float_time_to_text, highlight_rgb import locale import math import cairo @@ -37,7 +37,7 @@ class Bar(Graph): self.drawRectangle(cr, x, y, w, h) r, g, b = self.colorScheme[bar.yname] if bar.highlight: - r, g, b = self.colorScheme['__highlight'] + r, g, b = highlight_rgb(r, g, b) cr.set_source(self.sourceRectangle(x, y, w, h, r, g, b)) cr.fill_preserve() cr.stroke() diff --git a/tryton/gui/window/view_form/view/graph_gtk/graph.py b/tryton/gui/window/view_form/view/graph_gtk/graph.py index 76a4a05963..0d16e16324 100644 --- a/tryton/gui/window/view_form/view/graph_gtk/graph.py +++ b/tryton/gui/window/view_form/view/graph_gtk/graph.py @@ -454,7 +454,7 @@ class Graph(gtk.DrawingArea): color = self.attrs.get('color', 'blue') r, g, b = hex2rgb(COLOR_SCHEMES.get(color, color)) maxcolor = max(max(r, g), b) - self.colorScheme = generateColorscheme(color, keys + ['__highlight'], + self.colorScheme = generateColorscheme(color, keys, maxcolor / (len(keys) or 1)) for yfield in self.yfields: if yfield.get('color'): diff --git a/tryton/gui/window/view_form/view/graph_gtk/line.py b/tryton/gui/window/view_form/view/graph_gtk/line.py index 13868f4323..3be9eed0ef 100644 --- a/tryton/gui/window/view_form/view/graph_gtk/line.py +++ b/tryton/gui/window/view_form/view/graph_gtk/line.py @@ -3,7 +3,7 @@ #This code is inspired by the pycha project #(http://www.lorenzogil.com/projects/pycha/) from graph import Graph -from tryton.common import hex2rgb, float_time_to_text +from tryton.common import hex2rgb, highlight_rgb, float_time_to_text import locale import math import cairo @@ -146,7 +146,8 @@ class Line(Graph): point.y * self.area.h + self.area.y, 3, 0, 2 * math.pi) cr.stroke() - cr.set_source_rgb(*self.colorScheme['__highlight']) + cr.set_source_rgb(*highlight_rgb( + *self.colorScheme[point.yname])) cr.arc(point.x * self.area.w + self.area.x, point.y * self.area.h + self.area.y, 3, 0, 2 * math.pi) diff --git a/tryton/gui/window/view_form/view/graph_gtk/pie.py b/tryton/gui/window/view_form/view/graph_gtk/pie.py index 9018288c3d..24b6f6e141 100644 --- a/tryton/gui/window/view_form/view/graph_gtk/pie.py +++ b/tryton/gui/window/view_form/view/graph_gtk/pie.py @@ -5,7 +5,7 @@ from graph import Graph, Area import math import cairo -from tryton.common import hex2rgb, float_time_to_text +from tryton.common import hex2rgb, highlight_rgb, float_time_to_text import locale import tryton.rpc as rpc @@ -94,7 +94,7 @@ class Pie(Graph): if bool(int(self.yfields[0].get('fill', 1))): color = self.colorScheme[slice.xname] if slice.highlight: - color = self.colorScheme['__highlight'] + color = highlight_rgb(*color) cr.set_source_rgba(*color) slice.draw(cr, self.centerx, self.centery, self.radius) cr.fill()