Debian Codegraph @

src/graphics.rs

raw symbols SCIP
1 /*
2 * Copyright 2020-2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use gtk::gdk;
21
22 pub fn calculate_contrast_color(color: &gdk::RGBA) -> gdk::RGBA {
23 // While the two colours are labelled "light" and "dark" which one is
24 // which actually depends on how light or dark the base colour is.
25
26 let brightness = color.red() + color.green() + color.blue();
27 if brightness > (1.0 / 1.1) {
28 // Darken light colours.
29 gdk::RGBA::new(
30 0.9 * color.red(),
31 0.9 * color.green(),
32 0.9 * color.blue(),
33 1.0,
34 )
35 } else if brightness > 0.04 {
36 // Lighten darker colours.
37 gdk::RGBA::new(
38 1.1 * color.red(),
39 1.1 * color.green(),
40 1.1 * color.blue(),
41 1.0,
42 )
43 } else {
44 // Very dark colours, add rather than multiply.
45 gdk::RGBA::new(
46 0.04 + color.red(),
47 0.04 + color.green(),
48 0.04 + color.blue(),
49 1.0,
50 )
51 }
52 }
53
54 #[cfg(test)]
55 mod test {
56 use super::*;
57
58 #[test]
59 fn test_light_color() {
60 let default = gdk::RGBA::parse("#7590AE").unwrap();
61 assert_eq!(
62 calculate_contrast_color(&default).to_string(),
63 "rgb(105,130,157)"
64 );
65
66 let contrast = gdk::RGBA::parse("rgb(105,130,157)").unwrap();
67 assert_eq!(
68 calculate_contrast_color(&contrast).to_string(),
69 "rgb(95,117,141)"
70 );
71 }
72 }