summaryrefslogtreecommitdiff
path: root/src/gui/colors.cpp
blob: 9ab75a82191c53a673bd4a72c851122383e41953 (plain)
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
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Leo Tenenbaum
// This file is part of GraphColoring.
//
// GraphColoring 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 3 of the License, or
// (at your option) any later version.
//
// GraphColoring 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 GraphColoring.  If not, see <https://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////

#include "colors.hpp"
#include "utils/errors.hpp"

#include <iostream>


#define SHADE_FACTOR 1.5

namespace gui {
namespace colors {

void Unpack(Color color, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a)
{
    *r = (color >> 24) & 0xFF;
    *g = (color >> 16) & 0xFF;
    *b = (color >>  8) & 0xFF;
    *a = (color >>  0) & 0xFF;
}

Color Pack(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    return (r << 24) + (g << 16) + (b << 8) + (a << 0);
}

static uint8_t shade_channel(uint8_t channel, double factor)
{
    if (channel * factor > 0xFF)
        channel = 0xFF;
    else
        channel *= factor;
    return channel;
}

Color Shade(Color color, double factor)
{
    uint8_t r, g, b, a;
    Unpack(color, &r, &g, &b, &a);
    r = shade_channel(r, factor);
    g = shade_channel(g, factor);
    b = shade_channel(b, factor);
    a = shade_channel(a, factor);
    return Pack(r, g, b, a);
}

Color Light(Color color)
{
    return Shade(color, SHADE_FACTOR);
}

Color Dark(Color color)
{
    return Shade(color, 1/SHADE_FACTOR);
}

Color FromString(const std::string& str)
{
	uint8_t r, g, b, a;
	try
	{
		r = std::stoi(str.substr(1,2),nullptr,16);
		g = std::stoi(str.substr(3,2),nullptr,16);
		b = std::stoi(str.substr(5,2),nullptr,16);
		if (str.length() >= 9)
			a = std::stoi(str.substr(7,2),nullptr,16);
		else
			a = 255;

		return Pack(r, g, b, a);
	}
	catch (std::invalid_argument&)
	{
		utils::errors::Die("Invalid color string: " + str);
	}
	return 0;
}

Color FromAttribute(pugi::xml_attribute attr)
{
	return FromString(std::string(attr.value()));
}

} // namespace colors
} // namespace gui