Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_color.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxge/cfx_color.h"
8
9#include <algorithm>
10
11#include "third_party/base/notreached.h"
12
13// Color types are ordered by increasing number of components so we can choose
14// a best color type during some conversions.
16 "color type values must be ordered");
18 "color type values must be ordered");
20 "color type values must be ordered");
21
22namespace {
23
24bool InRange(float comp) {
25 return comp >= 0.0f && comp <= 1.0f;
26}
27
28CFX_Color ConvertCMYK2GRAY(float dC, float dM, float dY, float dK) {
29 if (!InRange(dC) || !InRange(dM) || !InRange(dY) || !InRange(dK))
31 return CFX_Color(
33 1.0f - std::min(1.0f, 0.3f * dC + 0.59f * dM + 0.11f * dY + dK));
34}
35
36CFX_Color ConvertGRAY2CMYK(float dGray) {
37 if (!InRange(dGray))
39 return CFX_Color(CFX_Color::Type::kCMYK, 0.0f, 0.0f, 0.0f, 1.0f - dGray);
40}
41
42CFX_Color ConvertGRAY2RGB(float dGray) {
43 if (!InRange(dGray))
45 return CFX_Color(CFX_Color::Type::kRGB, dGray, dGray, dGray);
46}
47
48CFX_Color ConvertRGB2GRAY(float dR, float dG, float dB) {
49 if (!InRange(dR) || !InRange(dG) || !InRange(dB))
51 return CFX_Color(CFX_Color::Type::kGray, 0.3f * dR + 0.59f * dG + 0.11f * dB);
52}
53
54CFX_Color ConvertCMYK2RGB(float dC, float dM, float dY, float dK) {
55 if (!InRange(dC) || !InRange(dM) || !InRange(dY) || !InRange(dK))
57 return CFX_Color(CFX_Color::Type::kRGB, 1.0f - std::min(1.0f, dC + dK),
58 1.0f - std::min(1.0f, dM + dK),
59 1.0f - std::min(1.0f, dY + dK));
60}
61
62CFX_Color ConvertRGB2CMYK(float dR, float dG, float dB) {
63 if (!InRange(dR) || !InRange(dG) || !InRange(dB))
65
66 float c = 1.0f - dR;
67 float m = 1.0f - dG;
68 float y = 1.0f - dB;
69 return CFX_Color(CFX_Color::Type::kCMYK, c, m, y,
70 std::min(c, std::min(m, y)));
71}
72
73} // namespace
74
75CFX_Color CFX_Color::ConvertColorType(Type nConvertColorType) const {
76 if (nColorType == nConvertColorType)
77 return *this;
78
79 CFX_Color ret;
80 switch (nColorType) {
82 ret = *this;
84 break;
86 switch (nConvertColorType) {
88 break;
90 NOTREACHED_NORETURN();
92 ret = ConvertGRAY2RGB(fColor1);
93 break;
95 ret = ConvertGRAY2CMYK(fColor1);
96 break;
97 }
98 break;
100 switch (nConvertColorType) {
102 break;
104 ret = ConvertRGB2GRAY(fColor1, fColor2, fColor3);
105 break;
107 NOTREACHED_NORETURN();
109 ret = ConvertRGB2CMYK(fColor1, fColor2, fColor3);
110 break;
111 }
112 break;
114 switch (nConvertColorType) {
116 break;
118 ret = ConvertCMYK2GRAY(fColor1, fColor2, fColor3, fColor4);
119 break;
121 ret = ConvertCMYK2RGB(fColor1, fColor2, fColor3, fColor4);
122 break;
124 NOTREACHED_NORETURN();
125 }
126 break;
127 }
128 return ret;
129}
130
131FX_COLORREF CFX_Color::ToFXColor(int32_t nTransparency) const {
132 CFX_Color ret;
133 switch (nColorType) {
136 break;
137 }
139 ret = ConvertGRAY2RGB(fColor1);
140 ret.fColor4 = nTransparency;
141 break;
142 }
145 ret.fColor4 = nTransparency;
146 break;
147 }
149 ret = ConvertCMYK2RGB(fColor1, fColor2, fColor3, fColor4);
150 ret.fColor4 = nTransparency;
151 break;
152 }
153 }
154 return ArgbEncode(ret.fColor4, static_cast<int32_t>(ret.fColor1 * 255),
155 static_cast<int32_t>(ret.fColor2 * 255),
156 static_cast<int32_t>(ret.fColor3 * 255));
157}
158
159CFX_Color CFX_Color::operator-(float fColorSub) const {
161 switch (nColorType) {
164 sRet.fColor1 = std::max(1.0f - fColorSub, 0.0f);
165 sRet.fColor2 = std::max(1.0f - fColorSub, 0.0f);
166 sRet.fColor3 = std::max(1.0f - fColorSub, 0.0f);
167 break;
171 sRet.fColor1 = std::max(fColor1 - fColorSub, 0.0f);
172 sRet.fColor2 = std::max(fColor2 - fColorSub, 0.0f);
173 sRet.fColor3 = std::max(fColor3 - fColorSub, 0.0f);
174 sRet.fColor4 = std::max(fColor4 - fColorSub, 0.0f);
175 break;
176 }
177 return sRet;
178}
179
180CFX_Color CFX_Color::operator/(float fColorDivide) const {
182 switch (nColorType) {
185 sRet.fColor1 = 1.0f / fColorDivide;
186 sRet.fColor2 = 1.0f / fColorDivide;
187 sRet.fColor3 = 1.0f / fColorDivide;
188 break;
192 sRet = *this;
193 sRet.fColor1 /= fColorDivide;
194 sRet.fColor2 /= fColorDivide;
195 sRet.fColor3 /= fColorDivide;
196 sRet.fColor4 /= fColorDivide;
197 break;
198 }
199 return sRet;
200}
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition fx_dib.h:118
CFX_Color operator/(float fColorDivide) const
CFX_Color operator-(float fColorSub) const
float fColor4
Definition cfx_color.h:58
float fColor3
Definition cfx_color.h:57
CFX_Color ConvertColorType(Type nConvertColorType) const
Definition cfx_color.cpp:75
Type nColorType
Definition cfx_color.h:54
CFX_Color & operator=(const CFX_Color &that)=default
FX_COLORREF ToFXColor(int32_t nTransparency) const
constexpr CFX_Color(Type type=CFX_Color::Type::kTransparent, float color1=0.0f, float color2=0.0f, float color3=0.0f, float color4=0.0f)
Definition cfx_color.h:27
float fColor2
Definition cfx_color.h:56
float fColor1
Definition cfx_color.h:55