forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_spaces.cpp
116 lines (98 loc) · 2.66 KB
/
color_spaces.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Aseprite
// Copyright (C) 2018-2022 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/color_spaces.h"
#include "app/doc.h"
#include "app/pref/preferences.h"
#include "app/ui/editor/editor.h"
#include "os/system.h"
#include "os/window.h"
namespace app {
// We use this variable to avoid accessing Preferences::instance()
// from background threads when a color conversion between color
// spaces must be done.
static bool g_manage = false;
void initialize_color_spaces(Preferences& pref)
{
g_manage = pref.color.manage();
pref.color.manage.AfterChange.connect(
[](bool manage){
g_manage = manage;
});
}
os::ColorSpaceRef get_screen_color_space()
{
return os::instance()->defaultWindow()->colorSpace();
}
os::ColorSpaceRef get_current_color_space()
{
#ifdef ENABLE_UI
if (auto editor = Editor::activeEditor())
return editor->document()->osColorSpace();
else
#endif
return get_screen_color_space();
}
gfx::ColorSpaceRef get_working_rgb_space_from_preferences()
{
if (Preferences::instance().color.manage()) {
const std::string name = Preferences::instance().color.workingRgbSpace();
if (name == "sRGB")
return gfx::ColorSpace::MakeSRGB();
std::vector<os::ColorSpaceRef> colorSpaces;
os::instance()->listColorSpaces(colorSpaces);
for (auto& cs : colorSpaces) {
if (cs->gfxColorSpace()->name() == name)
return cs->gfxColorSpace();
}
}
return gfx::ColorSpace::MakeNone();
}
//////////////////////////////////////////////////////////////////////
// Color conversion
ConvertCS::ConvertCS()
{
if (g_manage) {
auto srcCS = get_current_color_space();
auto dstCS = get_screen_color_space();
if (srcCS && dstCS)
m_conversion = os::instance()->convertBetweenColorSpace(srcCS, dstCS);
}
}
ConvertCS::ConvertCS(const os::ColorSpaceRef& srcCS,
const os::ColorSpaceRef& dstCS)
{
if (g_manage) {
m_conversion = os::instance()->convertBetweenColorSpace(srcCS, dstCS);
}
}
ConvertCS::ConvertCS(ConvertCS&& that)
: m_conversion(std::move(that.m_conversion))
{
}
gfx::Color ConvertCS::operator()(const gfx::Color c)
{
if (m_conversion) {
gfx::Color out;
m_conversion->convertRgba((uint32_t*)&out, (const uint32_t*)&c, 1);
return out;
}
else {
return c;
}
}
ConvertCS convert_from_current_to_screen_color_space()
{
return ConvertCS();
}
ConvertCS convert_from_custom_to_srgb(const os::ColorSpaceRef& from)
{
return ConvertCS(from,
os::instance()->makeColorSpace(gfx::ColorSpace::MakeSRGB()));
}
} // namespace app