/usr/share/pyshared/virtaal/views/theme.py is in virtaal 0.7.1-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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 Zuza Software Foundation
#
# This file is part of Virtaal.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import gtk
INVERSE = False
"""Whether we are currently in an inverse type of theme (lite text on dark
background).
Other code wanting to alter their behaviour on whether we run in an inverse
theme or not, can inspect this to know.
"""
_default_theme = {
# Generic styling for a URL
'url_fg': '#0000ff',
'subtle_fg': 'darkgrey',
# Colours for the selected placeable (not affected by its type)
'selected_placeable_fg': '#000000',
'selected_placeable_bg': '#90ee90',
# Red warning foreground colour for things like XML markup
'markup_warning_fg': '#8b0000', #darkred/#8b0000
'ph_placeable_bg': '#f7f7f7',
# warning background for things like no search result
'warning_bg': '#f66',
# row colour for fuzzy strings
'fuzzy_row_bg': 'grey',
# selector text box border
'selector_textbox': '#5096f3',
# diffing markup:
# background for insertion
'diff_insert_bg': '#a0ffa0',
# background for deletion
'diff_delete_bg': '#ccc',
# background for replacement (deletion+insertion)
'diff_replace_bg': '#ffff70',
}
_inverse_theme = {
'url_fg': '#aaaaff',
'subtle_fg': 'grey',
'selected_placeable_fg': '#ffffff',
'selected_placeable_bg': '#007010',
'markup_warning_fg': '#ffa0a0',
'ph_placeable_bg': '#101010',
'warning_bg': '#900',
'fuzzy_row_bg': '#474747',
'selector_textbox': '#cbdffb',
'diff_insert_bg': '#005500',
'diff_delete_bg': '#333',
'diff_replace_bg': '#4a4a00',
}
current_theme = _default_theme.copy()
def set_default():
global INVERSE
global current_theme
INVERSE = False
current_theme.update(_default_theme)
def set_inverse():
global INVERSE
global current_theme
INVERSE = True
current_theme.update(_inverse_theme)
def is_inverse(fg, bg):
"""Takes a guess at whether the given foreground and background colours
represents and inverse theme (light text on a dark background)."""
# Let's sum the three colour components to work out a rough idea of how
# "light" the colour is:
# TODO: consider using luminance calculation instead (probably overkill)
bg_sum = sum((bg.red, bg.green, bg.blue))
fg_sum = sum((fg.red, fg.green, fg.blue))
if bg_sum < fg_sum:
return True
else:
return False
def update_style(widget):
_style = widget.style
fg = _style.fg[gtk.STATE_NORMAL]
bg = _style.base[gtk.STATE_NORMAL]
if is_inverse(fg, bg):
set_inverse()
else:
set_default()
# On some themes (notably Windows XP with classic style), diff_delete_bg is
# almost identical to the background colour used. So we use something from
# the gtk theme that is supposed to be different, but not much.
if not has_reasonable_contrast(_style.bg[gtk.STATE_NORMAL], gtk.gdk.color_parse(current_theme['diff_delete_bg'])):
if INVERSE:
new_diff_delete_bg = _style.dark[gtk.STATE_NORMAL]
else:
new_diff_delete_bg = _style.light[gtk.STATE_NORMAL]
# we only want to change if it will actually result in something readable:
if has_good_contrast(_style.text[gtk.STATE_NORMAL], new_diff_delete_bg):
current_theme['diff_delete_bg'] = new_diff_delete_bg.to_string()
# these are based on an (old?) Web Content Accessibility Guidelines of the w3c
# See http://juicystudio.com/article/luminositycontrastratioalgorithm.php
# TODO: Might be a bit newer/better, so we shuld consider updating the code:
# http://www.w3.org/TR/WCAG20/Overview.html
def _luminance(c):
r = pow(c.red/65535.0, 2.2)
g = pow(c.green/65535.0, 2.2)
b = pow(c.blue/65535.0, 2.2)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
def _luminance_contrast_ratio(c1, c2):
l1 = _luminance(c1)
l2 = _luminance(c2)
l1, l2 = max(l1, l2), min(l1, l2)
return (l1 + 0.05) / (l2 + 0.05)
def has_good_contrast(c1, c2):
"""Takes a guess at whether the two given colours are in good contrast to
each other (for example, to be able to be used together as foreground and
background colour)."""
return _luminance_contrast_ratio(c1, c2) >= 4.5
def has_reasonable_contrast(c1, c2):
"""Similarly to has_good_contrast() this says whether the two given
colours have at least a reasonable amount of contrast, so that they would
be distinguishable."""
return _luminance_contrast_ratio(c1, c2) >= 1.2
# constant determined by testing in many themes, Windows XP with "classic"
# being the edge case
|