summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--makefile2
-rw-r--r--src/user/include/cxx/raleigh/w/colorpicker.h23
-rw-r--r--src/user/raleigh/w/colorpicker.cpp55
-rw-r--r--src/user/rhello/main.cpp19
4 files changed, 94 insertions, 5 deletions
diff --git a/makefile b/makefile
index 9fb6cd1..06cfeb0 100644
--- a/makefile
+++ b/makefile
@@ -106,7 +106,7 @@ obj/raleigh.so: obj/raleigh/runtime.po obj/raleigh/window.po \
obj/raleigh/widget.po obj/raleigh/util.po \
obj/raleigh/w/padding.po obj/raleigh/w/button.po \
obj/raleigh/w/vbox.po obj/raleigh/w/entry.po \
- obj/raleigh/w/label.po
+ obj/raleigh/w/label.po obj/raleigh/w/colorpicker.po
ld ${partlink} $^ -o $@
obj/init.elf: obj/init/init.o obj/knob.so obj/c.rto
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();