mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[python/kandinsky] Color can now be passed as a tuple
The values are expected to be integers between 0 and 255
This commit is contained in:
committed by
EmilieNumworks
parent
60cbc57a07
commit
bb89ae9211
@@ -1,9 +1,35 @@
|
||||
extern "C" {
|
||||
#include "modkandinsky.h"
|
||||
#include <py/objtuple.h>
|
||||
#include <py/runtime.h>
|
||||
}
|
||||
#include <kandinsky.h>
|
||||
#include "port.h"
|
||||
|
||||
static KDColor ColorForTuple(mp_obj_t tuple) {
|
||||
size_t len;
|
||||
mp_obj_t * elem;
|
||||
|
||||
mp_obj_get_array(tuple, &len, &elem);
|
||||
if (len != 3) {
|
||||
mp_raise_TypeError("color needs 3 components");
|
||||
}
|
||||
|
||||
return KDColor::RGB888(
|
||||
mp_obj_get_int(elem[0]),
|
||||
mp_obj_get_int(elem[1]),
|
||||
mp_obj_get_int(elem[2])
|
||||
);
|
||||
}
|
||||
|
||||
static mp_obj_t TupleForRGB(uint8_t r, uint8_t g, uint8_t b) {
|
||||
mp_obj_tuple_t * t = static_cast<mp_obj_tuple_t *>(MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)));
|
||||
t->items[0] = MP_OBJ_NEW_SMALL_INT(r);
|
||||
t->items[1] = MP_OBJ_NEW_SMALL_INT(g);
|
||||
t->items[2] = MP_OBJ_NEW_SMALL_INT(b);
|
||||
return MP_OBJ_FROM_PTR(t);
|
||||
}
|
||||
|
||||
/* KDIonContext::sharedContext needs to be set to the wanted Rect before
|
||||
* calling kandinsky_get_pixel, kandinsky_set_pixel and kandinsky_draw_string.
|
||||
* We do this here with displaySandbox(), which pushes the SandboxController on
|
||||
@@ -11,28 +37,25 @@ extern "C" {
|
||||
* KDIonContext::sharedContext is set to the frame of the last object drawn. */
|
||||
|
||||
mp_obj_t modkandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue) {
|
||||
return
|
||||
MP_OBJ_NEW_SMALL_INT(
|
||||
KDColor::RGB888(
|
||||
mp_obj_get_int(red),
|
||||
mp_obj_get_int(green),
|
||||
mp_obj_get_int(blue)
|
||||
)
|
||||
);
|
||||
return TupleForRGB(
|
||||
mp_obj_get_int(red),
|
||||
mp_obj_get_int(green),
|
||||
mp_obj_get_int(blue)
|
||||
);
|
||||
}
|
||||
|
||||
mp_obj_t modkandinsky_get_pixel(mp_obj_t x, mp_obj_t y) {
|
||||
KDColor c = KDIonContext::sharedContext()->getPixel(
|
||||
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y))
|
||||
);
|
||||
return MP_OBJ_NEW_SMALL_INT(c);
|
||||
return TupleForRGB(c.red(), c.green(), c.blue());
|
||||
}
|
||||
|
||||
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color) {
|
||||
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
|
||||
KDIonContext::sharedContext()->setPixel(
|
||||
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y)),
|
||||
KDColor::RGB16(mp_obj_get_int(color))
|
||||
ColorForTuple(color)
|
||||
);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user