diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/fxge/cfx_color.cpp | 176 | ||||
-rw-r--r-- | core/fxge/cfx_color.h | 57 |
2 files changed, 233 insertions, 0 deletions
diff --git a/core/fxge/cfx_color.cpp b/core/fxge/cfx_color.cpp new file mode 100644 index 0000000000..eebdd2cf1c --- /dev/null +++ b/core/fxge/cfx_color.cpp @@ -0,0 +1,176 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxge/cfx_color.h" + +#include <algorithm> + +namespace { + +bool InRange(float comp) { + return comp >= 0.0f && comp <= 1.0f; +} + +CFX_Color ConvertCMYK2GRAY(float dC, float dM, float dY, float dK) { + if (!InRange(dC) || !InRange(dM) || !InRange(dY) || !InRange(dK)) + return CFX_Color(COLORTYPE_GRAY); + return CFX_Color( + COLORTYPE_GRAY, + 1.0f - std::min(1.0f, 0.3f * dC + 0.59f * dM + 0.11f * dY + dK)); +} + +CFX_Color ConvertGRAY2CMYK(float dGray) { + if (!InRange(dGray)) + return CFX_Color(COLORTYPE_CMYK); + return CFX_Color(COLORTYPE_CMYK, 0.0f, 0.0f, 0.0f, 1.0f - dGray); +} + +CFX_Color ConvertGRAY2RGB(float dGray) { + if (!InRange(dGray)) + return CFX_Color(COLORTYPE_RGB); + return CFX_Color(COLORTYPE_RGB, dGray, dGray, dGray); +} + +CFX_Color ConvertRGB2GRAY(float dR, float dG, float dB) { + if (!InRange(dR) || !InRange(dG) || !InRange(dB)) + return CFX_Color(COLORTYPE_GRAY); + return CFX_Color(COLORTYPE_GRAY, 0.3f * dR + 0.59f * dG + 0.11f * dB); +} + +CFX_Color ConvertCMYK2RGB(float dC, float dM, float dY, float dK) { + if (!InRange(dC) || !InRange(dM) || !InRange(dY) || !InRange(dK)) + return CFX_Color(COLORTYPE_RGB); + return CFX_Color(COLORTYPE_RGB, 1.0f - std::min(1.0f, dC + dK), + 1.0f - std::min(1.0f, dM + dK), + 1.0f - std::min(1.0f, dY + dK)); +} + +CFX_Color ConvertRGB2CMYK(float dR, float dG, float dB) { + if (!InRange(dR) || !InRange(dG) || !InRange(dB)) + return CFX_Color(COLORTYPE_CMYK); + + float c = 1.0f - dR; + float m = 1.0f - dG; + float y = 1.0f - dB; + return CFX_Color(COLORTYPE_CMYK, c, m, y, std::min(c, std::min(m, y))); +} + +} // namespace + +CFX_Color CFX_Color::ConvertColorType(int32_t nConvertColorType) const { + if (nColorType == nConvertColorType) + return *this; + + CFX_Color ret; + switch (nColorType) { + case COLORTYPE_TRANSPARENT: + ret = *this; + ret.nColorType = COLORTYPE_TRANSPARENT; + break; + case COLORTYPE_GRAY: + switch (nConvertColorType) { + case COLORTYPE_RGB: + ret = ConvertGRAY2RGB(fColor1); + break; + case COLORTYPE_CMYK: + ret = ConvertGRAY2CMYK(fColor1); + break; + } + break; + case COLORTYPE_RGB: + switch (nConvertColorType) { + case COLORTYPE_GRAY: + ret = ConvertRGB2GRAY(fColor1, fColor2, fColor3); + break; + case COLORTYPE_CMYK: + ret = ConvertRGB2CMYK(fColor1, fColor2, fColor3); + break; + } + break; + case COLORTYPE_CMYK: + switch (nConvertColorType) { + case COLORTYPE_GRAY: + ret = ConvertCMYK2GRAY(fColor1, fColor2, fColor3, fColor4); + break; + case COLORTYPE_RGB: + ret = ConvertCMYK2RGB(fColor1, fColor2, fColor3, fColor4); + break; + } + break; + } + return ret; +} + +FX_COLORREF CFX_Color::ToFXColor(int32_t nTransparency) const { + CFX_Color ret; + switch (nColorType) { + case COLORTYPE_TRANSPARENT: { + ret = CFX_Color(COLORTYPE_TRANSPARENT, 0, 0, 0, 0); + break; + } + case COLORTYPE_GRAY: { + ret = ConvertGRAY2RGB(fColor1); + ret.fColor4 = nTransparency; + break; + } + case COLORTYPE_RGB: { + ret = CFX_Color(COLORTYPE_RGB, fColor1, fColor2, fColor3); + ret.fColor4 = nTransparency; + break; + } + case COLORTYPE_CMYK: { + ret = ConvertCMYK2RGB(fColor1, fColor2, fColor3, fColor4); + ret.fColor4 = nTransparency; + break; + } + } + return ArgbEncode(ret.fColor4, static_cast<int32_t>(ret.fColor1 * 255), + static_cast<int32_t>(ret.fColor2 * 255), + static_cast<int32_t>(ret.fColor3 * 255)); +} + +CFX_Color CFX_Color::operator-(float fColorSub) const { + CFX_Color sRet(nColorType); + switch (nColorType) { + case COLORTYPE_TRANSPARENT: + sRet.nColorType = COLORTYPE_RGB; + sRet.fColor1 = std::max(1.0f - fColorSub, 0.0f); + sRet.fColor2 = std::max(1.0f - fColorSub, 0.0f); + sRet.fColor3 = std::max(1.0f - fColorSub, 0.0f); + break; + case COLORTYPE_RGB: + case COLORTYPE_GRAY: + case COLORTYPE_CMYK: + sRet.fColor1 = std::max(fColor1 - fColorSub, 0.0f); + sRet.fColor2 = std::max(fColor2 - fColorSub, 0.0f); + sRet.fColor3 = std::max(fColor3 - fColorSub, 0.0f); + sRet.fColor4 = std::max(fColor4 - fColorSub, 0.0f); + break; + } + return sRet; +} + +CFX_Color CFX_Color::operator/(float fColorDivide) const { + CFX_Color sRet(nColorType); + switch (nColorType) { + case COLORTYPE_TRANSPARENT: + sRet.nColorType = COLORTYPE_RGB; + sRet.fColor1 = 1.0f / fColorDivide; + sRet.fColor2 = 1.0f / fColorDivide; + sRet.fColor3 = 1.0f / fColorDivide; + break; + case COLORTYPE_RGB: + case COLORTYPE_GRAY: + case COLORTYPE_CMYK: + sRet = *this; + sRet.fColor1 /= fColorDivide; + sRet.fColor2 /= fColorDivide; + sRet.fColor3 /= fColorDivide; + sRet.fColor4 /= fColorDivide; + break; + } + return sRet; +} diff --git a/core/fxge/cfx_color.h b/core/fxge/cfx_color.h new file mode 100644 index 0000000000..b92c0bcc28 --- /dev/null +++ b/core/fxge/cfx_color.h @@ -0,0 +1,57 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXGE_CFX_COLOR_H_ +#define CORE_FXGE_CFX_COLOR_H_ + +#include "core/fpdfdoc/cpdf_formcontrol.h" +#include "core/fxge/fx_dib.h" + +struct CFX_Color { + explicit CFX_Color(FX_COLORREF ref) + : CFX_Color(FXARGB_R(ref), FXARGB_G(ref), FXARGB_B(ref)) {} + + CFX_Color(int32_t type = COLORTYPE_TRANSPARENT, + float color1 = 0.0f, + float color2 = 0.0f, + float color3 = 0.0f, + float color4 = 0.0f) + : nColorType(type), + fColor1(color1), + fColor2(color2), + fColor3(color3), + fColor4(color4) {} + + CFX_Color(int32_t r, int32_t g, int32_t b) + : nColorType(COLORTYPE_RGB), + fColor1(r / 255.0f), + fColor2(g / 255.0f), + fColor3(b / 255.0f), + fColor4(0) {} + + CFX_Color operator/(float fColorDivide) const; + CFX_Color operator-(float fColorSub) const; + + CFX_Color ConvertColorType(int32_t other_nColorType) const; + + FX_COLORREF ToFXColor(int32_t nTransparency) const; + + void Reset() { + nColorType = COLORTYPE_TRANSPARENT; + fColor1 = 0.0f; + fColor2 = 0.0f; + fColor3 = 0.0f; + fColor4 = 0.0f; + } + + int32_t nColorType; + float fColor1; + float fColor2; + float fColor3; + float fColor4; +}; + +#endif // CORE_FXGE_CFX_COLOR_H_ |