diff options
author | Benji Dial <benji6283@gmail.com> | 2021-03-08 15:51:56 -0500 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2021-03-08 15:51:56 -0500 |
commit | af52ddac750311ace3bd997245771b26119e1659 (patch) | |
tree | 332c92a654ac97601be0e0a3ad2ff06d33bd645a /src | |
parent | 8221fd5451f094defa9866f98026b74a969f7693 (diff) | |
download | portland-os-af52ddac750311ace3bd997245771b26119e1659.tar.gz |
colorpicker for raleigh
Diffstat (limited to 'src')
-rw-r--r-- | src/user/include/cxx/raleigh/w/colorpicker.h | 23 | ||||
-rw-r--r-- | src/user/raleigh/w/colorpicker.cpp | 55 | ||||
-rw-r--r-- | src/user/rhello/main.cpp | 19 |
3 files changed, 93 insertions, 4 deletions
diff --git a/src/user/include/cxx/raleigh/w/colorpicker.h b/src/user/include/cxx/raleigh/w/colorpicker.h new file mode 100644 index 0000000..c33288e --- /dev/null +++ b/src/user/include/cxx/raleigh/w/colorpicker.h @@ -0,0 +1,23 @@ +#ifndef RALEIGH_W_COLORPICKER_H +#define RALEIGH_W_COLORPICKER_H + +#include <raleigh/widget.h> + +namespace raleigh { + class colorpicker : public widget { + public: + colorpicker(_pixel_t default_color=RGB(20, 70, 30), uint8_t resolution=4); + _pixel_t get_picked_color() __attribute__ ((pure)); + + void paint(_pixel_t *pixbuf, uint32_t pitch) override; + void handle_click(coord window_coords, enum mouse_packet::mouse_button click_type, bool up) override; + void notify_has_opaque_parent(widget *parent) override; + + private: + _pixel_t picked_color; + uint8_t resolution; + uint8_t inv_res; + }; +} + +#endif
\ No newline at end of file diff --git a/src/user/raleigh/w/colorpicker.cpp b/src/user/raleigh/w/colorpicker.cpp new file mode 100644 index 0000000..faa720e --- /dev/null +++ b/src/user/raleigh/w/colorpicker.cpp @@ -0,0 +1,55 @@ +#include <raleigh/w/colorpicker.h> + +namespace raleigh { + colorpicker::colorpicker(_pixel_t default_color, uint8_t resolution) + : picked_color(default_color), resolution(resolution), inv_res(256 / resolution) { + size = coord(inv_res * 2, inv_res * 2); + closest_opaque = this; + } + + __attribute__ ((pure)) + _pixel_t colorpicker::get_picked_color() { + return picked_color; + } + + void colorpicker::paint(_pixel_t *pixbuf, uint32_t pitch) { + _pixel_t *pb_ptr = pixbuf + window_offset.y * pitch + window_offset.x; + for (uint16_t b = 0; b < inv_res; ++b) + for (uint16_t g = 0; g < inv_res; ++g) + pb_ptr[b * pitch + g] = (_pixel_t){.r = picked_color.r, .g = g * resolution, .b = b * resolution}; + pb_ptr += inv_res; + for (uint16_t r = 0; r < inv_res; ++r) + for (uint16_t b = 0; b < inv_res; ++b) + pb_ptr[r * pitch + b] = (_pixel_t){.r = r * resolution, .g = picked_color.g, .b = b * resolution}; + pb_ptr += inv_res * (pitch - 1); + for (uint16_t g = 0; g < inv_res; ++g) + for (uint16_t r = 0; r < inv_res; ++r) + pb_ptr[g * pitch + r] = (_pixel_t){.r = r * resolution, .g = g * resolution, .b = picked_color.b}; + pb_ptr += inv_res; + for (uint16_t y = 0; y < inv_res; ++y) + for (uint16_t x = 0; x < inv_res; ++x) + pb_ptr[y * pitch + x] = picked_color; + } + + void colorpicker::handle_click(coord window_coords, enum mouse_packet::mouse_button click_type, bool up) { + if (up || (click_type != mouse_packet::LEFT)) + return; + window_coords.x -= window_offset.x; + window_coords.y -= window_offset.y; + if ((window_coords.x < inv_res) && (window_coords.y < inv_res)) { + picked_color.g = window_coords.x * resolution; + picked_color.b = window_coords.y * resolution; + } + else if (window_coords.y < inv_res) { + picked_color.b = (window_coords.x - inv_res) * resolution; + picked_color.r = window_coords.y * resolution; + } + else if (window_coords.x < inv_res) { + picked_color.r = window_coords.x * resolution; + picked_color.g = (window_coords.y - inv_res) * resolution; + } + w->notify_needs_paint(*this); + } + + void colorpicker::notify_has_opaque_parent(widget *parent) {} +}
\ No newline at end of file diff --git a/src/user/rhello/main.cpp b/src/user/rhello/main.cpp index 45329b5..22b4fd5 100644 --- a/src/user/rhello/main.cpp +++ b/src/user/rhello/main.cpp @@ -1,3 +1,4 @@ +#include <raleigh/w/colorpicker.h> #include <raleigh/w/padding.h> #include <raleigh/w/button.h> #include <raleigh/w/label.h> @@ -7,14 +8,20 @@ #include <raleigh/runtime.h> #include <raleigh/window.h> -#include <popups/info.h> +#include <knob/format.h> #include <pland/pcrt.h> using namespace raleigh; +colorpicker *cp; +label *p_l; window *p_w; void onclick(button &from) { + const _pixel_t pc = cp->get_picked_color(); + char *const text = format("Selected color is #%2x%2x%2x", pc.r, pc.g, pc.b); + p_l->change_value(text); + free_block(text); p_w->show(); } @@ -30,17 +37,21 @@ void main() { entry e(8, 31, "This window is made with the Raleigh widget toolkit for Portland OS.\n\nI am a text entry widget. My cursor can be used, but editing is not yet implemented."); padding pe(e, 2); + cp = new colorpicker(); + padding pcp(*cp, 2); + dllist<widget &> wl; - wl.add_front(pe); wl.add_front(pb); + wl.add_front(pcp); + wl.add_front(pe); wl.add_front(pl); vbox vb(wl); padding pvb(vb, 2); window w(pvb, RGB(bf, bf, bf), (bool (*)(window &))&__pcrt_quit); - label p_l("You clicked that button!"); - padding p_pl(p_l, 4); + p_l = new label(""); + padding p_pl(*p_l, 4); p_w = new window(p_pl); w.show(); |