summaryrefslogtreecommitdiffstats
path: root/src/libs/dimg/dcolor.cpp
blob: 5a363c784762fb4a5e2c94912b5e43498d2eb0d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-12-02
 * Description : 8-16 bits color container.
 *
 * Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * RGB<->HLS transformation algorithms are inspired from methods
 * describe at this url :
 * http://www.paris-pc-gis.com/MI_Enviro/Colors/color_models.htm
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// C++ includes.

#include <cmath>

// Local includes.

#include "ddebug.h"
#include "dcolor.h"

namespace Digikam
{

/*
DColor::DColor(const DColor& color)
{
    m_red        = color.m_red;
    m_green      = color.m_green;
    m_blue       = color.m_blue;
    m_alpha      = color.m_alpha;
    m_sixteenBit = color.m_sixteenBit;
}
*/

DColor::DColor(const TQColor& color, bool sixteenBit)
{
    // initialize as eight bit
    m_red        = color.red();
    m_green      = color.green();
    m_blue       = color.blue();
    m_alpha      = 255;
    m_sixteenBit = false;

    // convert to sixteen bit if requested
    if (sixteenBit)
        convertToSixteenBit();
}

/*
DColor& DColor::operator=(const DColor& color)
{
    m_red        = color.m_red;
    m_green      = color.m_green;
    m_blue       = color.m_blue;
    m_alpha      = color.m_alpha;
    m_sixteenBit = color.m_sixteenBit;
    return *this;
}
*/

TQColor DColor::getTQColor() const
{
    if (m_sixteenBit)
    {
        DColor eightBit(*this);
        eightBit.convertToEightBit();
        return eightBit.getTQColor();
    }

    return (TQColor(m_red, m_green, m_blue));
}

void DColor::convertToSixteenBit()
{
    if (m_sixteenBit)
        return;

    m_red        = (m_red + 1) * 256 - 1;
    m_green      = (m_green + 1) * 256 - 1;
    m_blue       = (m_blue  + 1) * 256 - 1;
    m_alpha      = (m_alpha + 1) * 256 - 1;
    m_sixteenBit = true;
}

void DColor::convertToEightBit()
{
    if (!m_sixteenBit)
        return;

    m_red        = (m_red + 1) / 256 - 1;
    m_green      = (m_green + 1) / 256 - 1;
    m_blue       = (m_blue  + 1) / 256 - 1;
    m_alpha      = (m_alpha + 1) / 256 - 1;
    m_sixteenBit = false;
}


void DColor::getHSL(int* h, int* s, int* l) const
{
    double min;
    double max;
    double red;
    double green;
    double blue;
    double delta;
    double sum;
    double hue, sat, lig;

    double range = m_sixteenBit ? 65535.0 : 255.0;

    red   = m_red   / range;
    green = m_green / range;
    blue  = m_blue  / range;

    if (red > green)
    {
        if (red > blue)
            max = red;
        else
            max = blue;

        if (green < blue)
            min = green;
        else
            min = blue;
    }
    else
    {
        if (green > blue)
            max = green;
        else
            max = blue;

        if (red < blue)
            min = red;
        else
            min = blue;
    }

    sum = max + min;

    lig = sum / 2;
    sat = 0;
    hue = 0;

    if (max != min)
    {
        delta = max - min;

        if (lig <= 0.5)
            sat = delta / sum;
        else
            sat = delta / (2 - sum);

        if (red == max)
            hue = (green - blue) / delta;
        else if (green == max)
            hue = 2 + (blue - red) / delta;
        else if (blue == max)
            hue = 4 + (red - green) / delta;

        if (hue < 0)
            hue += 6;
        if (hue > 6)
            hue -= 6;

        hue *= 60;
    }

    *h = lround(hue * range / 360.0);
    *s = lround(sat * range);
    *l = lround(lig * range);
}

void DColor::setRGB(int h, int s, int l, bool sixteenBit)
{
    double hue;
    double lightness;
    double saturation;
    double m1, m2;
    double r, g, b;

    double range = m_sixteenBit ? 65535.0 : 255.0;

    if (s == 0)
    {
        m_red   = l;
        m_green = l;
        m_blue  = l;
    }
    else
    {
        hue        = (double)(h * 360.0 / range);
        lightness  = (double)(l / range);
        saturation = (double)(s / range);

        if (lightness <= 0.5)
            m2 = lightness * (1 + saturation);
        else
            m2 = lightness + saturation - lightness * saturation;

        m1 = 2 * lightness - m2;

        double mh;

        mh = hue + 120;
        while (mh > 360)
            mh -= 360;
        while (mh < 0)
            mh += 360;

        if (mh < 60)
            r = m1 + (m2 - m1) * mh / 60;
        else if (mh < 180)
            r = m2;
        else if (mh < 240)
            r = m1 + (m2 - m1) * (240 - mh) / 60;
        else
            r = m1;

        mh = hue;
        while (mh > 360)
            mh -= 360;
        while (mh < 0)
            mh += 360;

        if (mh < 60)
            g = m1 + (m2 - m1) * mh / 60;
        else if (mh < 180)
            g = m2;
        else if (mh < 240)
            g = m1 + (m2 - m1) * (240 - mh) / 60;
        else
            g = m1;

        mh = hue - 120;
        while (mh > 360)
            mh -= 360;
        while (mh < 0)
            mh += 360;

        if (mh < 60)
            b = m1 + (m2 - m1) * mh / 60;
        else if (mh < 180)
            b = m2;
        else if (mh < 240)
            b = m1 + (m2 - m1) * (240 - mh) / 60;
        else
            b = m1;

        m_red   = lround(r * range);
        m_green = lround(g * range);
        m_blue  = lround(b * range);
    }

    m_sixteenBit = sixteenBit;

    // Fully opaque color.
    if (m_sixteenBit)
        m_alpha = 65535;
    else
        m_alpha = 255;
}

}  // NameSpace Digikam