mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[python] Color: avoid magic number 255, clean code of ColorParser and
Turtle::colormode
This commit is contained in:
committed by
LeaNumworks
parent
c920df1f76
commit
8f5fa50f22
@@ -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() {
|
||||
|
||||
@@ -181,7 +181,8 @@ void MicroPython::collectRootsAtAddress(char * address, int byteLength) {
|
||||
}
|
||||
|
||||
KDColor MicroPython::ColorParser::ParseColor(mp_obj_t input, ColorMode ColorMode){
|
||||
if(mp_obj_is_str(input)){
|
||||
static constexpr int maxColorIntensity = static_cast<int>(ColorMode::MaxIntensity255);
|
||||
if (mp_obj_is_str(input)) {
|
||||
size_t l;
|
||||
const char * color = mp_obj_str_get_data(input, &l);
|
||||
// TODO add cyan
|
||||
@@ -219,11 +220,11 @@ KDColor MicroPython::ColorParser::ParseColor(mp_obj_t input, ColorMode ColorMode
|
||||
}
|
||||
|
||||
mp_float_t GreyLevel = mp_obj_float_get(mp_parse_num_decimal(color, strlen(color), false, false, NULL));
|
||||
if(GreyLevel >= 0 && GreyLevel <= 1){
|
||||
if(GreyLevel >= 0.0 && GreyLevel <= 1.0){
|
||||
return KDColor::RGB888(
|
||||
255 * (float) GreyLevel,
|
||||
255 * (float) GreyLevel,
|
||||
255 * (float) GreyLevel
|
||||
maxColorIntensity * (float) GreyLevel,
|
||||
maxColorIntensity * (float) GreyLevel,
|
||||
maxColorIntensity * (float) GreyLevel
|
||||
);
|
||||
}
|
||||
mp_raise_ValueError("Grey levels are between 0.0 and 1.0");
|
||||
@@ -237,22 +238,14 @@ KDColor MicroPython::ColorParser::ParseColor(mp_obj_t input, ColorMode ColorMode
|
||||
mp_obj_get_array(input, &len, &elem);
|
||||
|
||||
if (len != 3) {
|
||||
mp_raise_TypeError("color needs 3 components");
|
||||
mp_raise_TypeError("Color needs 3 components");
|
||||
}
|
||||
|
||||
if(ColorMode == MicroPython::ColorParser::ColorMode::MaxIntensity1){
|
||||
return KDColor::RGB888(
|
||||
255 * mp_obj_get_float(elem[0]),
|
||||
255 * mp_obj_get_float(elem[1]),
|
||||
255 * mp_obj_get_float(elem[2])
|
||||
int intensityFactor = maxColorIntensity/static_cast<int>(ColorMode);
|
||||
return KDColor::RGB888(
|
||||
intensityFactor * mp_obj_get_float(elem[0]),
|
||||
intensityFactor * mp_obj_get_float(elem[1]),
|
||||
intensityFactor * mp_obj_get_float(elem[2])
|
||||
);
|
||||
} else {
|
||||
return KDColor::RGB888(
|
||||
mp_obj_get_int(elem[0]),
|
||||
mp_obj_get_int(elem[1]),
|
||||
mp_obj_get_int(elem[2])
|
||||
);
|
||||
}
|
||||
}
|
||||
mp_raise_TypeError("Color couldn't be parsed");
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ class ColorParser {
|
||||
|
||||
public:
|
||||
enum class ColorMode {
|
||||
MaxIntensity1,
|
||||
MaxIntensity255,
|
||||
MaxIntensity1 = 1,
|
||||
MaxIntensity255 = 255,
|
||||
};
|
||||
|
||||
static KDColor ParseColor(mp_obj_t input, ColorMode ColorMode = ColorMode::MaxIntensity255);
|
||||
|
||||
Reference in New Issue
Block a user