[python] Kandinsky module: color accepts "#FF0000", "red" kinds of

arguments
This commit is contained in:
Émilie Feral
2020-04-29 10:24:22 +02:00
committed by LeaNumworks
parent af0cdbcc1b
commit e7df25d558
3 changed files with 19 additions and 13 deletions

View File

@@ -6,11 +6,11 @@ extern "C" {
#include "port.h"
static mp_obj_t TupleForRGB(uint8_t r, uint8_t g, uint8_t b) {
static mp_obj_t TupleForRGB(KDColor c) {
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);
t->items[0] = MP_OBJ_NEW_SMALL_INT(c.red());
t->items[1] = MP_OBJ_NEW_SMALL_INT(c.green());
t->items[2] = MP_OBJ_NEW_SMALL_INT(c.blue());
return MP_OBJ_FROM_PTR(t);
}
@@ -20,12 +20,18 @@ static mp_obj_t TupleForRGB(uint8_t r, uint8_t g, uint8_t b) {
* the stackViewController and forces the window to redraw itself.
* 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 TupleForRGB(
mp_obj_get_int(red),
mp_obj_get_int(green),
mp_obj_get_int(blue)
);
mp_obj_t modkandinsky_color(size_t n_args, const mp_obj_t *args) {
mp_obj_t color;
if (n_args == 1) {
color = args[0];
} else if (n_args == 2) {
mp_raise_TypeError("color takes 1 or 3 arguments");
return mp_const_none;
} else {
assert(n_args == 3);
color = mp_obj_new_tuple(n_args, args);
}
return TupleForRGB(MicroPython::ColorParser::ParseColor(color));
}
/* Calling ExecutionEnvironment::displaySandbox() hides the console and switches
@@ -37,7 +43,7 @@ mp_obj_t modkandinsky_get_pixel(mp_obj_t x, mp_obj_t y) {
KDPoint point(mp_obj_get_int(x), mp_obj_get_int(y));
KDColor c;
KDIonContext::sharedContext()->getPixel(point, &c);
return TupleForRGB(c.red(), c.green(), c.blue());
return TupleForRGB(c);
}
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t input) {

View File

@@ -1,6 +1,6 @@
#include <py/obj.h>
mp_obj_t modkandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue);
mp_obj_t modkandinsky_color(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_get_pixel(mp_obj_t x, mp_obj_t y);
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color);
mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t *args);

View File

@@ -1,6 +1,6 @@
#include "modkandinsky.h"
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_color_obj, modkandinsky_color);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_color_obj, 1, 3, modkandinsky_color);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modkandinsky_get_pixel_obj, modkandinsky_get_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_set_pixel_obj, modkandinsky_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_string_obj, 3, 5, modkandinsky_draw_string);