summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/color-entry.cpp
blob: 804350c6b02957eb8dde4d42271268ce75ca7ae4 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Entry widget for typing color value in css form
 *//*
 * Authors:
 *   Tomasz Boczkowski <penginsbacon@gmail.com>
 *
 * Copyright (C) 2014 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <glibmm.h>
#include <glibmm/i18n.h>
#include <iomanip>

#include "color-entry.h"

namespace Inkscape {
namespace UI {
namespace Widget {

ColorEntry::ColorEntry(SelectedColor &color)
    : _color(color)
    , _updating(false)
    , _updatingrgba(false)
    , _prevpos(0)
    , _lastcolor(0)
{
    _color_changed_connection = color.signal_changed.connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged));
    _color_dragged_connection = color.signal_dragged.connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged));
    signal_activate().connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged));
    get_buffer()->signal_inserted_text().connect(sigc::mem_fun(this, &ColorEntry::_inputCheck));
    _onColorChanged();

    // add extra character for pasting a hash, '#11223344'
    set_max_length(9);
    set_width_chars(8);
    set_tooltip_text(_("Hexadecimal RGBA value of the color"));
}

ColorEntry::~ColorEntry()
{
    _color_changed_connection.disconnect();
    _color_dragged_connection.disconnect();
}

void ColorEntry::_inputCheck(guint pos, const gchar * /*chars*/, guint n_chars)
{
    // remember position of last character, so we can remove it.
    // we only overflow by 1 character at most.
    _prevpos = pos + n_chars - 1;
}

void ColorEntry::on_changed()
{
    if (_updating) {
        return;
    }
    if (_updatingrgba) {
        return;  // Typing text into entry box
    }

    Glib::ustring text = get_text();
    bool changed = false;

    // Coerce the value format to hexadecimal
    for (auto it = text.begin(); it != text.end(); /*++it*/) {
        if (!g_ascii_isxdigit(*it)) {
            text.erase(it);
            changed = true;
        } else {
            ++it;
        }
    }

    if (text.size() > 8) {
        text.erase(_prevpos, 1);
        changed = true;
    }

    // autofill rules
    gchar *str = g_strdup(text.c_str());
    gchar *end = nullptr;
    guint64 rgba = g_ascii_strtoull(str, &end, 16);
    ptrdiff_t len = end - str;
    if (len < 8) {
        if (len == 0) {
            rgba = _lastcolor;
        } else if (len <= 2) {
            if (len == 1) {
                rgba *= 17;
            }
            rgba = (rgba << 24) + (rgba << 16) + (rgba << 8);
        } else if (len <= 4) {
            // display as rrggbbaa
            rgba = rgba << (4 * (4 - len));
            guint64 r = rgba & 0xf000;
            guint64 g = rgba & 0x0f00;
            guint64 b = rgba & 0x00f0;
            guint64 a = rgba & 0x000f;
            rgba = 17 * ((r << 12) + (g << 8) + (b << 4) + a);
        } else {
            rgba = rgba << (4 * (8 - len));
        }

        if (len == 7) {
            rgba = (rgba & 0xfffffff0) + (_lastcolor & 0x00f);
        } else if (len == 5) {
            rgba = (rgba & 0xfffff000) + (_lastcolor & 0xfff);
        } else if (len != 4 && len != 8) {
            rgba = (rgba & 0xffffff00) + (_lastcolor & 0x0ff);
        }
    }

    _updatingrgba = true;
    if (changed) {
        set_text(str);
    }
    SPColor color(rgba);
    _color.setColorAlpha(color, SP_RGBA32_A_F(rgba));
    _updatingrgba = false;

    g_free(str);
}


void ColorEntry::_onColorChanged()
{
    if (_updatingrgba) {
        return;
    }

    SPColor color = _color.color();
    gdouble alpha = _color.alpha();

    _lastcolor = color.toRGBA32(alpha);
    Glib::ustring text = Glib::ustring::format(std::hex, std::setw(8), std::setfill(L'0'), _lastcolor);

    Glib::ustring old_text = get_text();
    if (old_text != text) {
        _updating = true;
        set_text(text);
        _updating = false;
    }
}
}
}
}
/*
  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 :