Debian Codegraph @

avogadro/core/contrastcolor.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_CONTRASTCOLOR_H
7 #define AVOGADRO_CORE_CONTRASTCOLOR_H
8
9 #include <avogadro/core/vector.h>
10
11 #include <cmath>
12
13 namespace Avogadro::Core {
14
15 inline Vector3ub contrastColor(const Vector3ub& rgb)
16 {
17 // If we're far 'enough' (+/-32) away from 128, just invert the component.
18 // If we're close to 128, inverting the color will end up too close to the
19 // input -- adjust the component before inverting.
20 const unsigned char minVal = 32;
21 const unsigned char maxVal = 223;
22 Vector3ub result;
23 for (size_t i = 0; i < 3; ++i) {
24 unsigned char input = rgb[i];
25 if (input > 160 || input < 96)
26 result[i] = static_cast<unsigned char>(255 - input);
27 else
28 result[i] = static_cast<unsigned char>(255 - (input / 4));
29
30 // Clamp to 32-->223 to prevent pure black/white
31 result[i] = std::clamp(result[i], minVal, maxVal);
32 }
33
34 return result;
35 }
36
37 } // namespace Avogadro::Core
38
39 #endif // AVOGADRO_CORE_CONTRASTCOLOR_H