summary refs log tree commit diff stats
path: root/pkgs/by-name/ba/back/src/git_bug/issue/label/mod.rs
blob: a971234b716d2966b3d78284d92bd4ac6d16a4a5 (plain) (blame)
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
// Back - An extremely simple git issue tracking system. Inspired by tvix's
// panettone
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of Back.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use std::fmt::Display;

use serde::Deserialize;
use sha2::{Digest, Sha256};

use crate::git_bug::format::HtmlString;

#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
pub struct Label {
    value: HtmlString,
}

impl Display for Label {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.value.fmt(f)
    }
}

impl Label {
    /// RGBA from a Label computed in a deterministic way
    /// This is taken completely from `git_bug`
    pub fn associate_color(&self) -> Color {
        // colors from: https://material-ui.com/style/color/
        let colors = vec![
            Color::from_rgba(244, 67, 54, 255),   // red
            Color::from_rgba(233, 30, 99, 255),   // pink
            Color::from_rgba(156, 39, 176, 255),  // purple
            Color::from_rgba(103, 58, 183, 255),  // deepPurple
            Color::from_rgba(63, 81, 181, 255),   // indigo
            Color::from_rgba(33, 150, 243, 255),  // blue
            Color::from_rgba(3, 169, 244, 255),   // lightBlue
            Color::from_rgba(0, 188, 212, 255),   // cyan
            Color::from_rgba(0, 150, 136, 255),   // teal
            Color::from_rgba(76, 175, 80, 255),   // green
            Color::from_rgba(139, 195, 74, 255),  // lightGreen
            Color::from_rgba(205, 220, 57, 255),  // lime
            Color::from_rgba(255, 235, 59, 255),  // yellow
            Color::from_rgba(255, 193, 7, 255),   // amber
            Color::from_rgba(255, 152, 0, 255),   // orange
            Color::from_rgba(255, 87, 34, 255),   // deepOrange
            Color::from_rgba(121, 85, 72, 255),   // brown
            Color::from_rgba(158, 158, 158, 255), // grey
            Color::from_rgba(96, 125, 139, 255),  // blueGrey
        ];

        let hash = Sha256::digest(self.to_string().as_bytes());

        let id: usize = hash
            .into_iter()
            .map(|val| val as usize)
            .fold(0, |acc, val| (acc + val) % colors.len());

        colors[id]
    }
}

#[derive(Default, Clone, Copy, Debug)]
pub struct Color {
    pub red: u32,
    pub green: u32,
    pub blue: u32,
    pub alpha: u32,
}

impl Color {
    pub fn from_rgba(red: u32, green: u32, blue: u32, alpha: u32) -> Self {
        Self {
            red,
            green,
            blue,
            alpha,
        }
    }
}