| 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 gtkrust-analyzer cargo gtk4 0.11.1 crate/::gdkrust-analyzer cargo gdk4 0.11.1 crate/; |
| 21 | |
| 22 | pub fn calculate_contrast_colorrust-analyzer cargo gnome-robots 50.0.0 graphics/calculate_contrast_color().(color: &gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#) -> gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/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.redrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]red().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().color.greenrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]green().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().color.bluerust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]blue().(); |
| 27 | if brightness rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().(1.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().1.1) { |
| 28 | // Darken light colours. |
| 29 | gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#::newrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]new().( |
| 30 | 0.9 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.redrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]red().(), |
| 31 | 0.9 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.greenrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]green().(), |
| 32 | 0.9 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.bluerust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]blue().(), |
| 33 | 1.0, |
| 34 | ) |
| 35 | } else if brightness rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().0.04 { |
| 36 | // Lighten darker colours. |
| 37 | gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#::newrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]new().( |
| 38 | 1.1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.redrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]red().(), |
| 39 | 1.1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.greenrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]green().(), |
| 40 | 1.1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().color.bluerust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]blue().(), |
| 41 | 1.0, |
| 42 | ) |
| 43 | } else { |
| 44 | // Very dark colours, add rather than multiply. |
| 45 | gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#::newrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]new().( |
| 46 | 0.04 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().color.redrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]red().(), |
| 47 | 0.04 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().color.greenrust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]green().(), |
| 48 | 0.04 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().color.bluerust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]blue().(), |
| 49 | 1.0, |
| 50 | ) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | mod testrust-analyzer cargo gnome-robots 50.0.0 graphics/test/ { |
| 56 | use superrust-analyzer cargo gnome-robots 50.0.0 graphics/::*; |
| 57 | |
| 58 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 59 | fn test_light_colorrust-analyzer cargo gnome-robots 50.0.0 graphics/test/test_light_color().() { |
| 60 | let default = gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#::parserust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]parse().("#7590AE").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 61 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 62 | calculate_contrast_colorrust-analyzer cargo gnome-robots 50.0.0 graphics/calculate_contrast_color().(&default).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 63 | "rgb(105,130,157)" |
| 64 | ); |
| 65 | |
| 66 | let contrast = gdkrust-analyzer cargo gdk4 0.11.1 crate/::RGBArust-analyzer cargo gdk4 0.11.1 auto/rgba/RGBA#::parserust-analyzer cargo gdk4 0.11.1 rgba/impl#[RGBA]parse().("rgb(105,130,157)").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 67 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 68 | calculate_contrast_colorrust-analyzer cargo gnome-robots 50.0.0 graphics/calculate_contrast_color().(&contrast).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 69 | "rgb(95,117,141)" |
| 70 | ); |
| 71 | } |
| 72 | } |