forked from aseprite/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsl.cpp
More file actions
88 lines (73 loc) · 1.81 KB
/
Copy pathhsl.cpp
File metadata and controls
88 lines (73 loc) · 1.81 KB
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
// LAF Gfx Library
// Copyright (c) 2020 Igara Studio S.A.
// Copyright (c) 2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/clamp.h"
#include "gfx/hsl.h"
#include "gfx/rgb.h"
#include <cmath>
namespace gfx {
using namespace std;
Hsl::Hsl(double hue, double saturation, double lightness)
: m_hue(hue)
, m_saturation(base::clamp(saturation, 0.0, 1.0))
, m_lightness(base::clamp(lightness, 0.0, 1.0))
{
while (m_hue < 0.0)
m_hue += 360.0;
m_hue = std::fmod(m_hue, 360.0);
}
Hsl::Hsl(const Rgb& rgb)
{
const int M = rgb.maxComponent();
const int m = rgb.minComponent();
const int c = M - m;
const double chroma = double(c) / 255.0;
const double l = double((M + m) / 255.0) / 2.0;
double hue_prime = 0.0;
double h, s;
if (c == 0) {
h = 0.0; // Undefined Hue because max == min
s = 0.0;
}
else {
const double r = double(rgb.red()) / 255.0;
const double g = double(rgb.green()) / 255.0;
const double b = double(rgb.blue()) / 255.0;
if (M == rgb.red()) {
hue_prime = (g - b) / chroma;
while (hue_prime < 0.0)
hue_prime += 6.0;
hue_prime = std::fmod(hue_prime, 6.0);
}
else if (M == rgb.green()) {
hue_prime = ((b - r) / chroma) + 2.0;
}
else if (M == rgb.blue()) {
hue_prime = ((r - g) / chroma) + 4.0;
}
h = hue_prime * 60.0;
s = chroma / (1-std::fabs(2.0*l-1.0));
}
m_hue = h;
m_saturation = s;
m_lightness = l;
}
int Hsl::hueInt() const
{
return int(std::floor(m_hue + 0.5));
}
int Hsl::saturationInt() const
{
return int(std::floor(m_saturation*100.0 + 0.5));
}
int Hsl::lightnessInt() const
{
return int(std::floor(m_lightness*100.0 + 0.5));
}
} // namespace gfx