From 4b20c691741f9e7e7cb06dfd591e1b9a93569573 Mon Sep 17 00:00:00 2001 From: Arthur Camouseigt Date: Thu, 28 May 2020 13:43:15 +0200 Subject: [PATCH] [python/modturtle.cpp] Added support for float parameter in colormode() The function can now accept the value 1.0 in addition to 1 Change-Id: I9a7021076844784ca997fc618253524089cbe855 (cherry picked from commit c69e1542ecc9a7e474a3f1e92cc97f644bf4b701) --- python/port/mod/turtle/modturtle.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python/port/mod/turtle/modturtle.cpp b/python/port/mod/turtle/modturtle.cpp index b8d4d1de8..e08aa4dbd 100644 --- a/python/port/mod/turtle/modturtle.cpp +++ b/python/port/mod/turtle/modturtle.cpp @@ -168,8 +168,16 @@ mp_obj_t modturtle_pencolor(size_t n_args, const mp_obj_t *args) { mp_obj_t modturtle_colormode(size_t n_args, const mp_obj_t *args) { if(n_args == 0){ return mp_obj_new_int_from_uint(static_cast(sTurtle.colorMode())); - } else{ - int colorMode = mp_obj_get_int(args[0]); + } else { + // To accept both colormode(1) and colormode(1.0) we try to get args[0] as both int and float + mp_float_t decimalOne = mp_obj_get_float(args[0]); + int colorMode; + // But only 1 is accepted as float, 255 must be int + if (decimalOne == 1.0) { + colorMode = static_cast(MicroPython::Color::Mode::MaxIntensity1); + } else { + colorMode = mp_obj_get_int(args[0]); + } if (colorMode != static_cast(MicroPython::Color::Mode::MaxIntensity1) && colorMode != static_cast(MicroPython::Color::Mode::MaxIntensity255)) { mp_raise_ValueError("Colormode can be 1 or 255");