LibreOffice Module tools (master) 1
color.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <algorithm>
23#include <iomanip>
24#include <sstream>
25#include <stdlib.h>
26
27#include <tools/color.hxx>
28#include <tools/helpers.hxx>
29#include <tools/long.hxx>
30#include <o3tl/string_view.hxx>
32
34{
35 R = sal_uInt8(std::clamp(R + cLumInc, 0, 255));
36 G = sal_uInt8(std::clamp(G + cLumInc, 0, 255));
37 B = sal_uInt8(std::clamp(B + cLumInc, 0, 255));
38}
39
41{
42 R = sal_uInt8(std::clamp(R - cLumDec, 0, 255));
43 G = sal_uInt8(std::clamp(G - cLumDec, 0, 255));
44 B = sal_uInt8(std::clamp(B - cLumDec, 0, 255));
45}
46
48{
49 if (nContDec)
50 {
51 const double fM = (128.0 - 0.4985 * nContDec) / 128.0;
52 const double fOff = 128.0 - fM * 128.0;
53
54 R = sal_uInt8(std::clamp(FRound(R * fM + fOff), tools::Long(0), tools::Long(255)));
55 G = sal_uInt8(std::clamp(FRound(G * fM + fOff), tools::Long(0), tools::Long(255)));
56 B = sal_uInt8(std::clamp(FRound(B * fM + fOff), tools::Long(0), tools::Long(255)));
57 }
58}
59
60// color space conversion
61
62void Color::RGBtoHSB( sal_uInt16& nHue, sal_uInt16& nSat, sal_uInt16& nBri ) const
63{
64 sal_uInt8 c[3];
65 sal_uInt8 cMax, cMin;
66
67 c[0] = R;
68 c[1] = G;
69 c[2] = B;
70
71 cMax = c[0];
72 if( c[1] > cMax )
73 cMax = c[1];
74 if( c[2] > cMax )
75 cMax = c[2];
76
77 // Brightness = max(R, G, B);
78 nBri = cMax * 100 / 255;
79
80 cMin = c[0];
81 if( c[1] < cMin )
82 cMin = c[1];
83 if( c[2] < cMin )
84 cMin = c[2];
85
86 sal_uInt8 cDelta = cMax - cMin;
87
88 // Saturation = max - min / max
89 if( nBri > 0 )
90 nSat = cDelta * 100 / cMax;
91 else
92 nSat = 0;
93
94 if( nSat == 0 )
95 nHue = 0; // Default = undefined
96 else
97 {
98 double dHue = 0.0;
99
100 if( c[0] == cMax )
101 {
102 dHue = static_cast<double>( c[1] - c[2] ) / static_cast<double>(cDelta);
103 }
104 else if( c[1] == cMax )
105 {
106 dHue = 2.0 + static_cast<double>( c[2] - c[0] ) / static_cast<double>(cDelta);
107 }
108 else if ( c[2] == cMax )
109 {
110 dHue = 4.0 + static_cast<double>( c[0] - c[1] ) / static_cast<double>(cDelta);
111 }
112 dHue *= 60.0;
113
114 if( dHue < 0.0 )
115 dHue += 360.0;
116
117 nHue = static_cast<sal_uInt16>(dHue);
118 }
119}
120
121Color Color::HSBtoRGB( sal_uInt16 nHue, sal_uInt16 nSat, sal_uInt16 nBri )
122{
123 sal_uInt8 cR=0,cG=0,cB=0;
124 sal_uInt8 nB = static_cast<sal_uInt8>( nBri * 255 / 100 );
125
126 if( nSat == 0 )
127 {
128 cR = nB;
129 cG = nB;
130 cB = nB;
131 }
132 else
133 {
134 double dH = nHue;
135 double f;
136 sal_uInt16 n;
137 if( dH == 360.0 )
138 dH = 0.0;
139
140 dH /= 60.0;
141 n = static_cast<sal_uInt16>(dH);
142 f = dH - n;
143
144 sal_uInt8 a = static_cast<sal_uInt8>( nB * ( 100 - nSat ) / 100 );
145 sal_uInt8 b = static_cast<sal_uInt8>( nB * ( 100 - ( static_cast<double>(nSat) * f ) ) / 100 );
146 sal_uInt8 c = static_cast<sal_uInt8>( nB * ( 100 - ( static_cast<double>(nSat) * ( 1.0 - f ) ) ) / 100 );
147
148 switch( n )
149 {
150 case 0: cR = nB; cG = c; cB = a; break;
151 case 1: cR = b; cG = nB; cB = a; break;
152 case 2: cR = a; cG = nB; cB = c; break;
153 case 3: cR = a; cG = b; cB = nB; break;
154 case 4: cR = c; cG = a; cB = nB; break;
155 case 5: cR = nB; cG = a; cB = b; break;
156 }
157 }
158
159 return Color( cR, cG, cB );
160}
161
162Color Color::STRtoRGB(std::u16string_view colorname)
163{
164 Color col;
165 if(colorname.empty()) return col;
166
167 switch(colorname.size()){
168 case 7:
169 col.mValue = o3tl::toUInt32(colorname.substr(1,6), 16);
170 break;
171 case 6:
172 col.mValue = o3tl::toUInt32(colorname, 16);
173 break;
174 case 4:
175 {
176 sal_Unicode data[6] = { colorname[1], colorname[1], colorname[2],
177 colorname[2], colorname[3], colorname[3] };
178 col.mValue = o3tl::toUInt32(std::u16string_view(data,6), 16);
179 break;
180 }
181 case 3:
182 {
183 sal_Unicode data[6] = { colorname[0], colorname[0], colorname[1],
184 colorname[1], colorname[2], colorname[2] };
185 col.mValue = o3tl::toUInt32(std::u16string_view(data,6), 16);
186 break;
187 }
188 default:
189 break;
190 }
191 return col;
192}
193
194OUString Color::AsRGBHexString() const
195{
196 std::stringstream ss;
197 ss << std::hex << std::setfill ('0') << std::setw(6) << sal_uInt32(GetRGBColor());
198 return OUString::createFromAscii(ss.str());
199}
200
201OUString Color::AsRGBHEXString() const
202{
203 std::stringstream ss;
204 ss << std::hex << std::uppercase << std::setfill ('0') << std::setw(6) << sal_uInt32(GetRGBColor());
205 return OUString::createFromAscii(ss.str());
206}
207
208void Color::ApplyTintOrShade(sal_Int16 n100thPercent)
209{
210 if (n100thPercent == 0)
211 return;
212
214 double fFactor = 1.0 - (std::abs(double(n100thPercent)) / 10000.0);
215 double fResult;
216
217 if (n100thPercent > 0) // tint
218 {
219 fResult = aBColor.getBlue() * fFactor + (1.0 - fFactor);
220 }
221 else // shade
222 {
223 fResult = aBColor.getBlue() * fFactor;
224 }
225
226 aBColor.setBlue(fResult);
227 aBColor = basegfx::utils::hsl2rgb(aBColor);
228
229 R = sal_uInt8(std::lround(aBColor.getRed() * 255.0));
230 G = sal_uInt8(std::lround(aBColor.getGreen() * 255.0));
231 B = sal_uInt8(std::lround(aBColor.getBlue() * 255.0));
232}
233
234void Color::ApplyLumModOff(sal_Int16 nMod, sal_Int16 nOff)
235{
236 if (nMod == 10000 && nOff == 0)
237 {
238 return;
239 }
240 // Switch to HSL, where applying these transforms is easier.
242
243 // 50% is half luminance, 200% is double luminance. Unit is 100th percent.
244 aBColor.setBlue(std::clamp(aBColor.getBlue() * nMod / 10000, 0.0, 1.0));
245 // If color changes to black or white, it will stay gray if luminance changes again.
246 if ((aBColor.getBlue() == 0.0) || (aBColor.getBlue() == 1.0))
247 {
248 aBColor.setGreen(0.0);
249 }
250
251 // Luminance offset means hue and saturation is left unchanged. Unit is 100th percent.
252 aBColor.setBlue(std::clamp(aBColor.getBlue() + static_cast<double>(nOff) / 10000, 0.0, 1.0));
253 // If color changes to black or white, it will stay gray if luminance changes again.
254 if ((aBColor.getBlue() == 0.0) || (aBColor.getBlue() == 1.0))
255 {
256 aBColor.setGreen(0.0);
257 }
258
259 // Switch back to RGB.
260 aBColor = basegfx::utils::hsl2rgb(aBColor);
261 R = sal_uInt8(std::lround(aBColor.getRed() * 255.0));
262 G = sal_uInt8(std::lround(aBColor.getGreen() * 255.0));
263 B = sal_uInt8(std::lround(aBColor.getBlue() * 255.0));
264}
265
266/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: color.hxx:51
Color GetRGBColor() const
Returns the same color but ignoring the transparency value.
Definition: color.hxx:218
void DecreaseContrast(sal_uInt8 cContDec)
Decreases color contrast with white by cContDec.
Definition: color.cxx:47
OUString AsRGBHEXString() const
Definition: color.cxx:201
sal_uInt8 B
Definition: color.hxx:63
void DecreaseLuminance(sal_uInt8 cLumDec)
Decreases the color luminance by cLumDec.
Definition: color.cxx:40
void RGBtoHSB(sal_uInt16 &nHue, sal_uInt16 &nSaturation, sal_uInt16 &nBrightness) const
Color space conversion tools.
Definition: color.cxx:62
sal_uInt8 R
Definition: color.hxx:65
basegfx::BColor getBColor() const
Definition: color.hxx:418
static Color STRtoRGB(std::u16string_view colorname)
Converts a string into a color.
Definition: color.cxx:162
OUString AsRGBHexString() const
Definition: color.cxx:194
void IncreaseLuminance(sal_uInt8 cLumInc)
Increases the color luminance by cLumInc.
Definition: color.cxx:33
sal_uInt8 G
Definition: color.hxx:64
void ApplyTintOrShade(sal_Int16 n100thPercent)
Apply tint or shade to a color.
Definition: color.cxx:208
static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness)
Color space conversion tools The range for h/s/b is:
Definition: color.cxx:121
void ApplyLumModOff(sal_Int16 nMod, sal_Int16 nOff)
Apply luminance offset and/or modulation.
Definition: color.cxx:234
constexpr Color()
Definition: color.hxx:72
double getBlue() const
void setBlue(double fNew)
double getRed() const
void setGreen(double fNew)
double getGreen() const
Degree100 abs(Degree100 x)
Definition: degree.hxx:42
tools::Long FRound(double fVal)
Definition: helpers.hxx:74
sal_Int64 n
uno_Any a
RttiCompleteObjectLocator col
BColor rgb2hsl(const BColor &rRGBColor)
BColor hsl2rgb(const BColor &rHSLColor)
sal_uInt32 toUInt32(std::u16string_view str, sal_Int16 radix=10)
long Long
Definition: long.hxx:34
unsigned char sal_uInt8
sal_uInt16 sal_Unicode