[python] Color: avoid magic number 255, clean code of ColorParser and

Turtle::colormode
This commit is contained in:
Émilie Feral
2020-04-29 10:10:12 +02:00
committed by LeaNumworks
parent c920df1f76
commit 8f5fa50f22
3 changed files with 21 additions and 29 deletions

View File

@@ -165,18 +165,17 @@ 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 (sTurtle.colorMode() == MicroPython::ColorParser::ColorMode::MaxIntensity255) ? mp_obj_new_int_from_uint(255) : mp_obj_new_int_from_uint(1);
return mp_obj_new_int_from_uint(static_cast<int>(sTurtle.colorMode()));
} else{
int colorMode = mp_obj_get_int(args[0]);
if(colorMode == 1){
sTurtle.setColorMode(MicroPython::ColorParser::ColorMode::MaxIntensity1);
} else if(colorMode == 255){
sTurtle.setColorMode(MicroPython::ColorParser::ColorMode::MaxIntensity255);
} else {
mp_raise_ValueError("Colormodes can be 1 or 255");
if (colorMode != static_cast<int>(MicroPython::ColorParser::ColorMode::MaxIntensity1) &&
colorMode != static_cast<int>(MicroPython::ColorParser::ColorMode::MaxIntensity255)) {
mp_raise_ValueError("Colormode can be 1 or 255");
return mp_const_none;
}
sTurtle.setColorMode(static_cast<MicroPython::ColorParser::ColorMode>(colorMode));
return mp_const_none;
}
return mp_const_none;
}
mp_obj_t modturtle_showturtle() {