| 1 |
|
| 2 |
|
| 3 |
from gi.repository import Gdk |
| 4 |
|
| 5 |
from .utilities_paths import utilities_get_rgba_for_xy |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
def utilities_gdk_rgba_to_color_array(gdk_rgba): |
| 10 |
"""Convert a Gdk.RGBA to an array of values intended to be used directly by |
| 11 |
cairo. Thus the value of alpha between 0 and 1, despite the 3 other values |
| 12 |
being integers between 0 and 255.""" |
| 13 |
r = int(255 * gdk_rgba.red) |
| 14 |
g = int(255 * gdk_rgba.green) |
| 15 |
b = int(255 * gdk_rgba.blue) |
| 16 |
return [r, g, b, gdk_rgba.alpha] |
| 17 |
|
| 18 |
def utilities_gdk_rgba_to_normalized_array(gdk_rgba): |
| 19 |
"""Convert a Gdk.RGBA to an array of values between 0 and 1.""" |
| 20 |
return [gdk_rgba.red, gdk_rgba.green, gdk_rgba.blue, gdk_rgba.alpha] |
| 21 |
|
| 22 |
def utilities_color_array_to_gdk_rgba(r, g, b, a): |
| 23 |
"""Convert an array of values (as used by cairo) to a Gdk.RGBA object. The |
| 24 |
values should be integers between 0 and 255, except alpha.""" |
| 25 |
if a > 1.0: |
| 26 |
raise Exception("Incorrect value for alpha") |
| 27 |
return Gdk.RGBA(red=r / 255, green=g / 255, blue=b / 255, alpha=a) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def utilities_rgba_to_hexadecimal(r, g, b, a): |
| 32 |
"""Methods such as `GdkPixbuf.Pixbuf.fill` wants an hexadecimal integer |
| 33 |
whose format is 0xrrggbbaa so here are ugly binary operators. |
| 34 |
All four values (even alpha!) must be between 0 and 255.""" |
| 35 |
r, g, b, a = int(r), int(g), int(b), int(a) |
| 36 |
return (((((r << 8) + g) << 8) + b) << 8) + a |
| 37 |
|
| 38 |
def utilities_rgb_to_hexadecimal(r, g, b): |
| 39 |
"""Methods such as `GdkPixbuf.Pixbuf.composite_color_simple` want an |
| 40 |
hexadecimal integer whose format is 0xaarrggbb so here are ugly binary |
| 41 |
operators.""" |
| 42 |
return (r << 16) + (g << 8) + b |
| 43 |
|
| 44 |
def utilities_gdk_rgba_to_hexadecimal(gdk_rgba): |
| 45 |
"""Returns a displayable string of the hexadecimal code corresponding to a |
| 46 |
Gdk.RGBA object.""" |
| 47 |
r = int(255 * gdk_rgba.red) |
| 48 |
g = int(255 * gdk_rgba.green) |
| 49 |
b = int(255 * gdk_rgba.blue) |
| 50 |
hexa_raw = hex(utilities_rgb_to_hexadecimal(r, g, b)) |
| 51 |
hexa_string = str(hexa_raw)[2:] |
| 52 |
hexa_string = "0" * (6 - len(hexa_string)) + hexa_string |
| 53 |
return "#" + hexa_string |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def utilities_gdk_rgba_from_xy(surface, event_x, event_y): |
| 58 |
rgba_vals = utilities_get_rgba_for_xy(surface, event_x, event_y) |
| 59 |
if rgba_vals is None: |
| 60 |
return |
| 61 |
rgba_vals = [*rgba_vals] |
| 62 |
rgba_vals[3] /= 255 |
| 63 |
return utilities_color_array_to_gdk_rgba(*rgba_vals) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def utilities_get_rgba_name(gdk_rgba): |
| 68 |
"""To improve accessibility, it is useful to display the name of the colors. |
| 69 |
Sadly, it's a mess to implement, and it's quite approximative.""" |
| 70 |
[red, green, blue, alpha] = utilities_gdk_rgba_to_normalized_array(gdk_rgba) |
| 71 |
color_string = "" |
| 72 |
alpha_string = "" |
| 73 |
if alpha == 0.0: |
| 74 |
return _("Transparent") |
| 75 |
elif alpha < 1.0: |
| 76 |
alpha_string = " - " + _("%s%% transparent") % int(100 - alpha * 100) |
| 77 |
|
| 78 |
total = red + green + blue |
| 79 |
orange_coef = 0.0 |
| 80 |
lumin = total / 3.0 |
| 81 |
|
| 82 |
if green != 0: |
| 83 |
orange_coef = (red/green) * lumin |
| 84 |
|
| 85 |
if total != 0: |
| 86 |
rgb_percents = [red/total, green/total, blue/total] |
| 87 |
else: |
| 88 |
rgb_percents = [0.333, 0.333, 0.333] |
| 89 |
|
| 90 |
|
| 91 |
grey_coef_r = rgb_percents[0] * lumin / 3 |
| 92 |
grey_coef_g = rgb_percents[1] * lumin / 3 |
| 93 |
grey_coef_b = rgb_percents[2] * lumin / 3 |
| 94 |
is_grey = abs(grey_coef_r - grey_coef_g) < 0.01 |
| 95 |
is_grey = is_grey and abs(grey_coef_g - grey_coef_b) < 0.01 |
| 96 |
is_grey = is_grey and abs(grey_coef_b - grey_coef_r) < 0.01 |
| 97 |
|
| 98 |
if is_grey: |
| 99 |
if lumin > 0.9: |
| 100 |
color_string = _("White") |
| 101 |
elif lumin < 0.1: |
| 102 |
color_string = _("Black") |
| 103 |
else: |
| 104 |
color_string = _("Grey") |
| 105 |
|
| 106 |
elif rgb_percents[0] > 0.5 and rgb_percents[1] > 0.2 and rgb_percents[1] < 0.4: |
| 107 |
if orange_coef > 0.87: |
| 108 |
color_string = _("Orange") |
| 109 |
else: |
| 110 |
color_string = _("Brown") |
| 111 |
|
| 112 |
elif rgb_percents[0] > 0.4 and rgb_percents[1] < 0.3 and rgb_percents[2] < 0.3: |
| 113 |
if lumin < 0.7 and rgb_percents[0] < 0.7: |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
color_string = _("Probably brown") |
| 118 |
else: |
| 119 |
color_string = _("Red") |
| 120 |
elif rgb_percents[1] > 0.4 and rgb_percents[0] < 0.4 and rgb_percents[2] < 0.4: |
| 121 |
color_string = _("Green") |
| 122 |
elif rgb_percents[2] > 0.4 and rgb_percents[0] < 0.3 and rgb_percents[1] < 0.4: |
| 123 |
color_string = _("Blue") |
| 124 |
|
| 125 |
elif rgb_percents[0] > 0.3 and rgb_percents[1] > 0.3 and rgb_percents[2] < 0.3: |
| 126 |
if rgb_percents[1] < 0.4: |
| 127 |
color_string = _("Probably brown") |
| 128 |
else: |
| 129 |
color_string = _("Yellow") |
| 130 |
elif rgb_percents[0] > 0.3 and rgb_percents[2] > 0.3 and rgb_percents[1] < 0.3: |
| 131 |
if lumin > 0.6 and rgb_percents[1] < 0.1: |
| 132 |
color_string = _("Magenta") |
| 133 |
else: |
| 134 |
color_string = _("Purple") |
| 135 |
elif rgb_percents[1] > 0.3 and rgb_percents[2] > 0.3 and rgb_percents[0] < 0.2: |
| 136 |
if lumin > 0.7: |
| 137 |
color_string = _("Cyan") |
| 138 |
else: |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
color_string = _("Probably teal") |
| 145 |
|
| 146 |
else: |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
color_string = _("Unknown color name") |
| 151 |
|
| 152 |
|
| 153 |
return (color_string + alpha_string) |
| 154 |
|
| 155 |
|
| 156 |
|