forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_utils.cpp
189 lines (158 loc) · 4.54 KB
/
color_utils.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Aseprite
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// 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.h"
#include "app/color_utils.h"
#include "app/modules/palettes.h"
#include "gfx/hsv.h"
#include "gfx/rgb.h"
#include "doc/image.h"
#include "doc/layer.h"
#include "doc/palette.h"
#include "doc/sprite.h"
namespace app {
gfx::Color color_utils::blackandwhite(gfx::Color color)
{
if ((gfx::getr(color)*30+gfx::getg(color)*59+gfx::getb(color)*11)/100 < 128)
return gfx::rgba(0, 0, 0);
else
return gfx::rgba(255, 255, 255);
}
gfx::Color color_utils::blackandwhite_neg(gfx::Color color)
{
if ((gfx::getr(color)*30+gfx::getg(color)*59+gfx::getb(color)*11)/100 < 128)
return gfx::rgba(255, 255, 255);
else
return gfx::rgba(0, 0, 0);
}
gfx::Color color_utils::color_for_ui(const app::Color& color)
{
gfx::Color c = gfx::ColorNone;
switch (color.getType()) {
case app::Color::MaskType:
c = gfx::ColorNone;
break;
case app::Color::RgbType:
case app::Color::HsvType:
case app::Color::HslType:
c = gfx::rgba(
color.getRed(),
color.getGreen(),
color.getBlue(),
color.getAlpha());
break;
case app::Color::GrayType:
c = gfx::rgba(
color.getGray(),
color.getGray(),
color.getGray(),
color.getAlpha());
break;
case app::Color::IndexType: {
int i = color.getIndex();
ASSERT(i >= 0 && i < (int)get_current_palette()->size());
uint32_t _c = get_current_palette()->getEntry(i);
c = gfx::rgba(
rgba_getr(_c),
rgba_getg(_c),
rgba_getb(_c),
color.getAlpha());
break;
}
}
return c;
}
doc::color_t color_utils::color_for_image(const app::Color& color, PixelFormat format)
{
if (color.getType() == app::Color::MaskType)
return 0;
doc::color_t c = -1;
switch (format) {
case IMAGE_RGB:
c = doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
break;
case IMAGE_GRAYSCALE:
c = doc::graya(color.getGray(), color.getAlpha());
break;
case IMAGE_INDEXED:
c = color.getIndex();
break;
}
return c;
}
doc::color_t color_utils::color_for_image_without_alpha(const app::Color& color, PixelFormat format)
{
if (color.getType() == app::Color::MaskType)
return 0;
doc::color_t c = -1;
switch (format) {
case IMAGE_RGB:
c = doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), 255);
break;
case IMAGE_GRAYSCALE:
c = doc::graya(color.getGray(), 255);
break;
case IMAGE_INDEXED:
c = color.getIndex();
break;
}
return c;
}
doc::color_t color_utils::color_for_layer(const app::Color& color, Layer* layer)
{
return color_for_target(color, ColorTarget(layer));
}
doc::color_t color_utils::color_for_target_mask(const app::Color& color, const ColorTarget& colorTarget)
{
int c = -1;
if (color.getType() == app::Color::MaskType) {
c = colorTarget.maskColor();
}
else {
switch (colorTarget.pixelFormat()) {
case IMAGE_RGB:
c = doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
break;
case IMAGE_GRAYSCALE:
c = doc::graya(color.getGray(), color.getAlpha());
break;
case IMAGE_INDEXED:
if (color.getType() == app::Color::IndexType) {
c = color.getIndex();
}
else {
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int a = color.getAlpha();
int mask = (colorTarget.isTransparent() ?
colorTarget.maskColor(): // Don't return the mask color
-1);
c = get_current_palette()->findExactMatch(r, g, b, a, mask);
if (c < 0)
c = get_current_palette()->findBestfit(r, g, b, a, mask);
}
break;
}
}
return (doc::color_t)c;
}
// TODO remove this function using a special RGB background layer (24bpp or 32bpp ignoring alpha)
doc::color_t color_utils::color_for_target(const app::Color& color, const ColorTarget& colorTarget)
{
doc::color_t c = color_utils::color_for_target_mask(color, colorTarget);
if (colorTarget.isBackground()) {
switch (colorTarget.pixelFormat()) {
case IMAGE_RGB: c |= doc::rgba_a_mask; break;
case IMAGE_GRAYSCALE: c |= doc::graya_a_mask; break;
}
}
return c;
}
} // namespace app