summaryrefslogtreecommitdiffstats
path: root/src/ui/selected-color.cpp
blob: d8bbab1bcd8d238459fb110a00b34b44c5494673 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Color selected in color selector widget.
 * This file was created during the refactoring of SPColorSelector
 *//*
 * Authors:
 *	 bulia byak <buliabyak@users.sf.net>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Tomasz Boczkowski <penginsbacon@gmail.com>
 *
 * Copyright (C) 2014 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glibmm/ustring.h>
#include <cmath>

#include "svg/svg-icc-color.h"
#include "ui/selected-color.h"

namespace Inkscape {
namespace UI {

double const SelectedColor::_EPSILON = 1e-4;

SelectedColor::SelectedColor()
	: _color(0)
    , _alpha(1.0)
    , _held(false)
    , _virgin(true)
    , _updating(false)
{

}

SelectedColor::~SelectedColor() = default;

void SelectedColor::setColor(SPColor const &color)
{
    setColorAlpha( color, _alpha);
}

SPColor SelectedColor::color() const
{
    return _color;
}

void SelectedColor::setAlpha(gfloat alpha)
{
    g_return_if_fail( ( 0.0 <= alpha ) && ( alpha <= 1.0 ) );
    setColorAlpha( _color, alpha);
}

gfloat SelectedColor::alpha() const
{
    return _alpha;
}

void SelectedColor::setValue(guint32 value)
{
    SPColor color(value);
    gfloat alpha = SP_RGBA32_A_F(value);
    setColorAlpha(color, alpha);
}

guint32 SelectedColor::value() const
{
    return color().toRGBA32(_alpha);
}

void SelectedColor::setColorAlpha(SPColor const &color, gfloat alpha, bool emit_signal)
{
#ifdef DUMP_CHANGE_INFO
    g_message("SelectedColor::setColorAlpha( this=%p, %f, %f, %f, %s,   %f,   %s)", this, color.v.c[0], color.v.c[1], color.v.c[2], (color.icc?color.icc->colorProfile.c_str():"<null>"), alpha, (emit_signal?"YES":"no"));
#endif
    g_return_if_fail( ( 0.0 <= alpha ) && ( alpha <= 1.0 ) );

    if (_updating) {
        return;
    }

#ifdef DUMP_CHANGE_INFO
    g_message("---- SelectedColor::setColorAlpha    virgin:%s   !close:%s    alpha is:%s",
              (_virgin?"YES":"no"),
              (!color.isClose( _color, _EPSILON )?"YES":"no"),
              ((fabs((_alpha) - (alpha)) >= _EPSILON )?"YES":"no")
              );
#endif

    if ( _virgin || !color.isClose( _color, _EPSILON ) ||
         (fabs((_alpha) - (alpha)) >= _EPSILON )) {

        _virgin = false;

        _color = color;
        _alpha = alpha;

        if (emit_signal)
        {
            _updating = true;
            if (_held) {
                signal_dragged.emit();
            } else {
                signal_changed.emit();
            }
            _updating = false;
        }

#ifdef DUMP_CHANGE_INFO
    } else {
        g_message("++++ SelectedColor::setColorAlpha   color:%08x  ==>  _color:%08X   isClose:%s", color.toRGBA32(alpha), _color.toRGBA32(_alpha),
                  (color.isClose( _color, _EPSILON )?"YES":"no"));
#endif
    }
}

void SelectedColor::colorAlpha(SPColor &color, gfloat &alpha) const {
	color = _color;
	alpha = _alpha;
}

void SelectedColor::setHeld(bool held) {
    if (_updating) {
        return;
    }
    bool grabbed = held && !_held;
    bool released = !held && _held;

    _held = held;

    _updating = true;
    if (grabbed) {
        signal_grabbed.emit();
    }

    if (released) {
        signal_released.emit();
        // signal_changed.emit();  // TODO: signal_changed isn't emitted after dragging!
    }
    _updating = false;
}

void SelectedColor::preserveICC() {
    _color.icc = _color.icc ? new SVGICCColor(*_color.icc) : nullptr;
}

}
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :