Bump version to 6.4-15
[LibreOffice.git] / tools / source / generic / color.cxx
blob061435ed61c9d50f5f7c6c4a628b172064014ffa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <algorithm>
23 #include <iomanip>
24 #include <sstream>
25 #include <stdlib.h>
27 #include <tools/color.hxx>
28 #include <tools/helpers.hxx>
29 #include <basegfx/color/bcolortools.hxx>
31 void Color::IncreaseLuminance(sal_uInt8 cLumInc)
33 R = sal_uInt8(std::clamp(long(R) + cLumInc, 0L, 255L));
34 G = sal_uInt8(std::clamp(long(G) + cLumInc, 0L, 255L));
35 B = sal_uInt8(std::clamp(long(B) + cLumInc, 0L, 255L));
38 void Color::DecreaseLuminance(sal_uInt8 cLumDec)
40 R = sal_uInt8(std::clamp(long(R) - cLumDec, 0L, 255L));
41 G = sal_uInt8(std::clamp(long(G) - cLumDec, 0L, 255L));
42 B = sal_uInt8(std::clamp(long(B) - cLumDec, 0L, 255L));
45 void Color::DecreaseContrast(sal_uInt8 nContDec)
47 if (nContDec)
49 const double fM = (128.0 - 0.4985 * nContDec) / 128.0;
50 const double fOff = 128.0 - fM * 128.0;
52 R = sal_uInt8(std::clamp(FRound(R * fM + fOff), 0L, 255L));
53 G = sal_uInt8(std::clamp(FRound(G * fM + fOff), 0L, 255L));
54 B = sal_uInt8(std::clamp(FRound(B * fM + fOff), 0L, 255L));
58 bool Color::IsDark() const
60 return GetLuminance() <= 60;
63 bool Color::IsBright() const
65 return GetLuminance() >= 245;
68 // color space conversion
70 void Color::RGBtoHSB( sal_uInt16& nHue, sal_uInt16& nSat, sal_uInt16& nBri ) const
72 sal_uInt8 c[3];
73 sal_uInt8 cMax, cMin;
75 c[0] = R;
76 c[1] = G;
77 c[2] = B;
79 cMax = c[0];
80 if( c[1] > cMax )
81 cMax = c[1];
82 if( c[2] > cMax )
83 cMax = c[2];
85 // Brightness = max(R, G, B);
86 nBri = cMax * 100 / 255;
88 cMin = c[0];
89 if( c[1] < cMin )
90 cMin = c[1];
91 if( c[2] < cMin )
92 cMin = c[2];
94 sal_uInt8 cDelta = cMax - cMin;
96 // Saturation = max - min / max
97 if( nBri > 0 )
98 nSat = cDelta * 100 / cMax;
99 else
100 nSat = 0;
102 if( nSat == 0 )
103 nHue = 0; // Default = undefined
104 else
106 double dHue = 0.0;
108 if( c[0] == cMax )
110 dHue = static_cast<double>( c[1] - c[2] ) / static_cast<double>(cDelta);
112 else if( c[1] == cMax )
114 dHue = 2.0 + static_cast<double>( c[2] - c[0] ) / static_cast<double>(cDelta);
116 else if ( c[2] == cMax )
118 dHue = 4.0 + static_cast<double>( c[0] - c[1] ) / static_cast<double>(cDelta);
120 dHue *= 60.0;
122 if( dHue < 0.0 )
123 dHue += 360.0;
125 nHue = static_cast<sal_uInt16>(dHue);
129 Color Color::HSBtoRGB( sal_uInt16 nHue, sal_uInt16 nSat, sal_uInt16 nBri )
131 sal_uInt8 cR=0,cG=0,cB=0;
132 sal_uInt8 nB = static_cast<sal_uInt8>( nBri * 255 / 100 );
134 if( nSat == 0 )
136 cR = nB;
137 cG = nB;
138 cB = nB;
140 else
142 double dH = nHue;
143 double f;
144 sal_uInt16 n;
145 if( dH == 360.0 )
146 dH = 0.0;
148 dH /= 60.0;
149 n = static_cast<sal_uInt16>(dH);
150 f = dH - n;
152 sal_uInt8 a = static_cast<sal_uInt8>( nB * ( 100 - nSat ) / 100 );
153 sal_uInt8 b = static_cast<sal_uInt8>( nB * ( 100 - ( static_cast<double>(nSat) * f ) ) / 100 );
154 sal_uInt8 c = static_cast<sal_uInt8>( nB * ( 100 - ( static_cast<double>(nSat) * ( 1.0 - f ) ) ) / 100 );
156 switch( n )
158 case 0: cR = nB; cG = c; cB = a; break;
159 case 1: cR = b; cG = nB; cB = a; break;
160 case 2: cR = a; cG = nB; cB = c; break;
161 case 3: cR = a; cG = b; cB = nB; break;
162 case 4: cR = c; cG = a; cB = nB; break;
163 case 5: cR = nB; cG = a; cB = b; break;
167 return Color( cR, cG, cB );
170 OUString Color::AsRGBHexString() const
172 std::stringstream ss;
173 ss << std::hex << std::setfill ('0') << std::setw(6) << sal_uInt32(GetRGBColor());
174 return OUString::createFromAscii(ss.str().c_str());
177 void Color::ApplyTintOrShade(sal_Int16 n100thPercent)
179 if (n100thPercent == 0)
180 return;
182 basegfx::BColor aBColor = basegfx::utils::rgb2hsl(getBColor());
183 double fFactor = 1.0 - (std::abs(double(n100thPercent)) / 10000.0);
184 double fResult;
186 if (n100thPercent > 0) // tint
188 fResult = aBColor.getBlue() * fFactor + (1.0 - fFactor);
190 else // shade
192 fResult = aBColor.getBlue() * fFactor;
195 aBColor.setBlue(fResult);
196 aBColor = basegfx::utils::hsl2rgb(aBColor);
198 R = sal_uInt8(std::lround(aBColor.getRed() * 255.0));
199 G = sal_uInt8(std::lround(aBColor.getGreen() * 255.0));
200 B = sal_uInt8(std::lround(aBColor.getBlue() * 255.0));
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */