Commit f66f4693 authored by Rahix's avatar Rahix
Browse files

WIP: Add add_stop

Code duplication hell
parent 707c6c14
Pipeline #5442 failed with stages
in 3 minutes and 37 seconds
......@@ -308,6 +308,54 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mp_ctx_stroke_color_obj, 2, 3, mp_ctx_stroke_color
);
static mp_obj_t mp_ctx_add_stop(size_t n_args, const mp_obj_t *args)
{
mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]);
mp_obj_t color_in = args[2];
float pos = mp_obj_get_float(args[1]);
float alpha_f = 1.0;
if (n_args == 4) {
alpha_f = mp_obj_get_float(args[3]);
}
if (alpha_f < 0.0 || alpha_f > 1.0) {
mp_raise_ValueError("alpha must be between 0.0 or 1.0");
}
mp_obj_t red_in, green_in, blue_in;
if (mp_obj_get_int(mp_obj_len(color_in)) < 3) {
mp_raise_ValueError("color must have 3 elements");
}
red_in = mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL);
green_in = mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL);
blue_in = mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL);
/*
* The color can be either floats between 0 and 1 or integers between 0
* and 255. Make this decision based on the first element we find.
*/
if (mp_obj_is_type(red_in, &mp_type_float)) {
float red, green, blue;
red = mp_obj_get_float(red_in);
green = mp_obj_get_float(green_in);
blue = mp_obj_get_float(blue_in);
ctx_gradient_add_stop(self->ctx, pos, red, green, blue, alpha_f);
} else {
uint8_t red, green, blue, alpha;
red = mp_obj_get_int(red_in);
green = mp_obj_get_int(green_in);
blue = mp_obj_get_int(blue_in);
alpha = (int)(alpha_f * 255.0);
ctx_gradient_add_stop_u8(self->ctx, pos, red, green, blue, alpha);
}
return MP_OBJ_FROM_PTR(self);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_add_stop_obj, 3, 4, mp_ctx_add_stop);
static mp_obj_t mp_ctx_update(mp_obj_t self_in, mp_obj_t display_in)
{
mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in);
......@@ -447,6 +495,7 @@ static const mp_rom_map_elem_t mp_ctx_locals_dict_table[] = {
MP_CTX_METHOD(linear_gradient),
MP_CTX_METHOD(radial_gradient),
MP_CTX_METHOD(line_dash),
MP_CTX_METHOD(add_stop),
MP_CTX_METHOD(color),
MP_CTX_METHOD(stroke_color),
......
......@@ -287,6 +287,7 @@ Q(radial_gradient)
Q(text_width)
Q(fill_text)
Q(stroke_text)
Q(add_stop)
Q(FILL_RULE_WINDING)
Q(FILL_RULE_EVEN_ODD)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment