1 | // Aseprite |
2 | // Copyright (C) 2001-2017 David Capello |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include <cstdlib> |
12 | #include <string> |
13 | |
14 | #include "app/ui/hex_color_entry.h" |
15 | #include "base/hex.h" |
16 | #include "gfx/border.h" |
17 | #include "ui/message.h" |
18 | #include "ui/scale.h" |
19 | |
20 | namespace app { |
21 | |
22 | using namespace ui; |
23 | |
24 | HexColorEntry::CustomEntry::CustomEntry() |
25 | : Entry(16, "" ) |
26 | { |
27 | } |
28 | |
29 | bool HexColorEntry::CustomEntry::onProcessMessage(ui::Message* msg) |
30 | { |
31 | switch (msg->type()) { |
32 | case kMouseDownMessage: |
33 | setFocusStop(true); |
34 | requestFocus(); |
35 | break; |
36 | case kFocusLeaveMessage: |
37 | setFocusStop(false); |
38 | break; |
39 | } |
40 | return Entry::onProcessMessage(msg); |
41 | } |
42 | |
43 | HexColorEntry::HexColorEntry() |
44 | : Box(HORIZONTAL) |
45 | , m_label("#" ) |
46 | { |
47 | addChild(&m_label); |
48 | addChild(&m_entry); |
49 | |
50 | m_entry.Change.connect(&HexColorEntry::onEntryChange, this); |
51 | m_entry.setFocusStop(false); |
52 | |
53 | initTheme(); |
54 | |
55 | setBorder(gfx::Border(2*ui::guiscale(), 0, 0, 0)); |
56 | setChildSpacing(0); |
57 | } |
58 | |
59 | void HexColorEntry::setColor(const app::Color& color) |
60 | { |
61 | m_entry.setTextf("%02x%02x%02x" , |
62 | color.getRed(), |
63 | color.getGreen(), |
64 | color.getBlue()); |
65 | } |
66 | |
67 | void HexColorEntry::onEntryChange() |
68 | { |
69 | std::string text = m_entry.text(); |
70 | int r, g, b; |
71 | |
72 | // Remove non hex digits |
73 | while (text.size() > 0 && !base::is_hex_digit(text[0])) |
74 | text.erase(0, 1); |
75 | |
76 | // Fill with zeros at the end of the text |
77 | while (text.size() < 6) |
78 | text.push_back('0'); |
79 | |
80 | // Convert text (Base 16) to integer |
81 | int hex = std::strtol(text.c_str(), NULL, 16); |
82 | |
83 | r = (hex & 0xff0000) >> 16; |
84 | g = (hex & 0xff00) >> 8; |
85 | b = (hex & 0xff); |
86 | |
87 | ColorChange(app::Color::fromRgb(r, g, b)); |
88 | } |
89 | |
90 | } // namespace app |
91 | |