Krita Source Code Documentation
Loading...
Searching...
No Matches
KoColorSpacePreserveLightnessUtils.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Peter Schatz <voronwe13@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7
8#ifndef KOCOLORSPACEPRESERVELIGHTNESSUTILS_H
9#define KOCOLORSPACEPRESERVELIGHTNESSUTILS_H
10
11#include <KoColorSpaceMaths.h>
12#include "kis_global.h"
13
14template<typename CSTraits>
15inline static void fillGrayBrushWithColorPreserveLightnessRGB(quint8 *pixels, const QRgb *brush, quint8 *brushColor, qreal strength, qint32 nPixels) {
16 using RGBPixel = typename CSTraits::Pixel;
17 using channels_type = typename CSTraits::channels_type;
18 static const quint32 pixelSize = CSTraits::pixelSize;
19
20 const RGBPixel *srcColorRGB = reinterpret_cast<const RGBPixel*>(brushColor);
21
22 const float srcColorR = KoColorSpaceMaths<channels_type, float>::scaleToA(srcColorRGB->red);
23 const float srcColorG = KoColorSpaceMaths<channels_type, float>::scaleToA(srcColorRGB->green);
24 const float srcColorB = KoColorSpaceMaths<channels_type, float>::scaleToA(srcColorRGB->blue);
25 const float srcColorA = KoColorSpaceMaths<channels_type, float>::scaleToA(srcColorRGB->alpha);
26
44 const float srcColorL = getLightness<HSLType, float>(srcColorR, srcColorG, srcColorB);
45 const float lightnessB = 4 * srcColorL - 1;
46 const float lightnessA = 1 - lightnessB;
47
48 for (; nPixels > 0; --nPixels, pixels += pixelSize, ++brush) {
49 float brushMaskL = qRed(*brush) / 255.0f;
50 brushMaskL = (brushMaskL - 0.5) * strength + 0.5;
51 const float finalAlpha = qMin(qAlpha(*brush) / 255.0f, srcColorA);
52 float finalLightness = lightnessA * pow2(brushMaskL) + lightnessB * brushMaskL;
53 finalLightness = qBound(0.0f, finalLightness, 1.0f);
54
55 float pixelR = srcColorR;
56 float pixelG = srcColorG;
57 float pixelB = srcColorB;
58
59 setLightness<HSLType, float>(pixelR, pixelG, pixelB, finalLightness);
60
61 RGBPixel *pixelRGB = reinterpret_cast<RGBPixel*>(pixels);
65 pixelRGB->alpha = KoColorSpaceMaths<quint8, channels_type>::scaleToA(quint8(finalAlpha * 255));
66 }
67}
68
69template<typename CSTraits>
70inline static void modulateLightnessByGrayBrushRGB(quint8 *pixels, const QRgb *brush, qreal strength, qint32 nPixels) {
71 using RGBPixel = typename CSTraits::Pixel;
72 using channels_type = typename CSTraits::channels_type;
73 static const quint32 pixelSize = CSTraits::pixelSize;
74
75
93 for (; nPixels > 0; --nPixels, pixels += pixelSize, ++brush) {
94
95 RGBPixel *pixelRGB = reinterpret_cast<RGBPixel*>(pixels);
96
97 const float srcColorR = KoColorSpaceMaths<channels_type, float>::scaleToA(pixelRGB->red);
98 const float srcColorG = KoColorSpaceMaths<channels_type, float>::scaleToA(pixelRGB->green);
99 const float srcColorB = KoColorSpaceMaths<channels_type, float>::scaleToA(pixelRGB->blue);
100 //const float srcColorA = KoColorSpaceMaths<channels_type, float>::scaleToA(pixelRGB->alpha);
101
102 const float srcColorL = getLightness<HSLType, float>(srcColorR, srcColorG, srcColorB);
103 float brushMaskL = qRed(*brush) / 255.0f;
104 brushMaskL = (brushMaskL - 0.5) * strength * qAlpha(*brush) / 255.0 + 0.5;
105
106 const float lightnessB = 4 * srcColorL - 1;
107 const float lightnessA = 1 - lightnessB;
108
109 float finalLightness = lightnessA * pow2(brushMaskL) + lightnessB * brushMaskL;
110 finalLightness = qBound(0.0f, finalLightness, 1.0f);
111
112 float pixelR = srcColorR;
113 float pixelG = srcColorG;
114 float pixelB = srcColorB;
115
116 setLightness<HSLType, float>(pixelR, pixelG, pixelB, finalLightness);
117
121 //pixelRGB->alpha = KoColorSpaceMaths<quint8, channels_type>::scaleToA(quint8(finalAlpha * 255));
122 }
123}
124
125
126#endif // KOCOLORSPACEPRESERVELIGHTNESSUTILS_H
static void modulateLightnessByGrayBrushRGB(quint8 *pixels, const QRgb *brush, qreal strength, qint32 nPixels)
static void fillGrayBrushWithColorPreserveLightnessRGB(quint8 *pixels, const QRgb *brush, quint8 *brushColor, qreal strength, qint32 nPixels)
static _Tdst scaleToA(_T a)
unsigned int QRgb
T pow2(const T &x)
Definition kis_global.h:166