Refactor usage of colors and color picking

Member variables of state changed from an array of floats to a Color
struct.
This commit is contained in:
Stephen Seo 2024-01-23 14:53:47 +09:00
parent 2e3a2385f2
commit c6dd68ae14
3 changed files with 25 additions and 87 deletions

View file

@ -94,54 +94,16 @@ namespace Tri {
}
}
inline Color float_color_to_color(std::array<float, 4> fc) {
return Color {
(unsigned char)(fc[0] * 255.0F),
(unsigned char)(fc[1] * 255.0F),
(unsigned char)(fc[2] * 255.0F),
(unsigned char)(fc[3] * 255.0F)
};
}
inline Color float3_color_to_color(std::array<float, 3> fc) {
return Color {
(unsigned char)(fc[0] * 255.0F),
(unsigned char)(fc[1] * 255.0F),
(unsigned char)(fc[2] * 255.0F),
255
};
}
inline std::array<float, 4> color_to_float_color(Color c) {
return {
((float)c.r) / 255.0F,
((float)c.g) / 255.0F,
((float)c.b) / 255.0F,
((float)c.a) / 255.0F
};
}
inline std::array<float, 3> color_to_float3_color(Color c) {
return {
((float)c.r) / 255.0F,
((float)c.g) / 255.0F,
((float)c.b) / 255.0F
};
}
inline void draw_color_picker(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_COLOR_P)) {
GuiSetAlpha(1.0f);
if(!GuiWindowBox({4.0f, 4.0f, 242.0f, 292.0f}, "Tri Color Picker")) {
auto &colorArray = state->get_color();
// TODO this is called every draw, maybe use a designated member variable.
Color color = float_color_to_color(colorArray);
GuiColorPicker(
{8.0f, 32.0f, 206.0f, 240.0f},
nullptr,
&color);
colorArray = color_to_float_color(color);
if(GuiButton({8.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
&colorArray);
if(GuiButton({8.0f, 274.0f, 234.0f, 16.0f}, "Close")) {
state->close_color_picker();
}
} else {
@ -155,14 +117,11 @@ namespace Tri {
GuiSetAlpha(1.0f);
if(!GuiWindowBox({250.0f, 4.0f, 242.0f, 292.0f}, "BG Color Picker")) {
auto &colorArray = state->get_bg_color();
// TODO this is called every draw, maybe use a designated member variable.
Color color = float3_color_to_color(colorArray);
GuiColorPicker(
{254.0f, 32.0f, 206.0f, 240.0f},
nullptr,
&color);
colorArray = color_to_float3_color(color);
if(GuiButton({254.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
&colorArray);
if(GuiButton({254.0f, 274.0f, 234.0f, 16.0f}, "Close")) {
state->close_bg_color_picker();
}
} else {
@ -291,13 +250,10 @@ namespace Tri {
GuiSetAlpha(1.0f);
if(!GuiWindowBox({500.0f, 4.0f, 242.0f, 292.0f}, "Edit Tri Color Picker")) {
auto &colorArray = state->get_selected_tri_color();
// TODO this is called every draw, maybe use a designated member variable.
Color color = float_color_to_color(colorArray);
GuiColorPicker(
{504.0f, 32.0f, 206.0f, 240.0f},
nullptr,
&color);
colorArray = color_to_float_color(color);
&colorArray);
if(GuiButton({504.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_selected_tri_mode();
}

View file

@ -27,15 +27,15 @@ currentTri(),
currentTri_state(CurrentState::NONE),
currentTri_maxState(CurrentState::NONE),
pointCircle(),
colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f},
bgColorPickerColor{0.0f, 0.0f, 0.0f},
colorPickerColor{255, 255, 255, 255},
bgColorPickerColor{0, 0, 0, 255},
bgColor(BLACK),
saveFilenameBuffer(),
failedMessage(),
drawCache(),
pi(std::acos(-1.0f)),
selectedTri(),
selectedTriColor(),
selectedTriColor{255, 255, 255, 255},
selectedTriBlinkTimer(),
inputWidth(800),
inputHeight(600)
@ -229,10 +229,8 @@ void Tri::State::handle_events() {
my = drawImage.height - my;
colorPickerColor[0] = (float)(colors[mx + my * drawImage.width].r) / 255.0F;
colorPickerColor[1] = (float)(colors[mx + my * drawImage.width].g) / 255.0F;
colorPickerColor[2] = (float)(colors[mx + my * drawImage.width].b) / 255.0F;
colorPickerColor[3] = 1.0f;
colorPickerColor = colors[mx + my * drawImage.width];
pointCircle.fillColor = colors[mx + my * drawImage.width];
flags.reset(F_COPY_COLOR_MODE);
set_notification_text("Color set");
@ -255,10 +253,7 @@ void Tri::State::handle_events() {
flags.set(F_TRI_EDIT_MODE);
flags.set(F_TRI_EDIT_DRAW_TRI);
selectedTriBlinkTimer = 1.0f;
selectedTriColor[0] = tris[i].fillColor.r / 255.0f;
selectedTriColor[1] = tris[i].fillColor.g / 255.0f;
selectedTriColor[2] = tris[i].fillColor.b / 255.0f;
selectedTriColor[3] = tris[i].fillColor.a / 255.0f;
selectedTriColor = tris[i].fillColor;
break;
}
}
@ -281,18 +276,13 @@ void Tri::State::update() {
if(flags.test(F_COLOR_P_COLOR_DIRTY)) {
flags.reset(F_COLOR_P_COLOR_DIRTY);
pointCircle.fillColor = Color{
(unsigned char)(255 * colorPickerColor[0]),
(unsigned char)(255 * colorPickerColor[1]),
(unsigned char)(255 * colorPickerColor[2]),
(unsigned char)(255 * colorPickerColor[3])};
pointCircle.fillColor = colorPickerColor;
}
if(flags.test(F_BG_COLOR_P_COLOR_DIRTY)) {
flags.reset(F_BG_COLOR_P_COLOR_DIRTY);
bgColor.r = (unsigned char)(255 * bgColorPickerColor[0]);
bgColor.g = (unsigned char)(255 * bgColorPickerColor[1]);
bgColor.b = (unsigned char)(255 * bgColorPickerColor[2]);
bgColor = bgColorPickerColor;
bgColor.a = 255;
}
if(flags.test(F_TRI_EDIT_MODE)) {
@ -413,12 +403,12 @@ void Tri::State::append_notification_text(const char *text) {
notificationAlpha = 1.0f;
}
std::array<float, 4>& Tri::State::get_color() {
Color& Tri::State::get_color() {
flags.set(F_COLOR_P_COLOR_DIRTY);
return colorPickerColor;
}
std::array<float, 3>& Tri::State::get_bg_color() {
Color& Tri::State::get_bg_color() {
flags.set(F_BG_COLOR_P_COLOR_DIRTY);
return bgColorPickerColor;
}
@ -543,21 +533,13 @@ float Tri::State::get_pi() const {
return pi;
}
std::array<float, 4>& Tri::State::get_selected_tri_color() {
tris.at(selectedTri).fillColor = Color{
(unsigned char)(255.0f * selectedTriColor[0]),
(unsigned char)(255.0f * selectedTriColor[1]),
(unsigned char)(255.0f * selectedTriColor[2]),
(unsigned char)(255.0f * selectedTriColor[3])};
Color& Tri::State::get_selected_tri_color() {
tris.at(selectedTri).fillColor = selectedTriColor;
return selectedTriColor;
}
void Tri::State::close_selected_tri_mode() {
tris.at(selectedTri).fillColor = Color{
(unsigned char)(255.0f * selectedTriColor[0]),
(unsigned char)(255.0f * selectedTriColor[1]),
(unsigned char)(255.0f * selectedTriColor[2]),
(unsigned char)(255.0f * selectedTriColor[3])};
tris.at(selectedTri).fillColor = selectedTriColor;
flags.set(F_DRAW_CACHE_DIRTY);
reset_modes();
}

View file

@ -57,8 +57,8 @@ namespace Tri {
CurrentState currentTri_maxState;
Circle pointCircle;
std::array<float, 4> colorPickerColor;
std::array<float, 3> bgColorPickerColor;
Color colorPickerColor;
Color bgColorPickerColor;
Color bgColor;
std::array<char, 256> saveFilenameBuffer;
@ -69,7 +69,7 @@ namespace Tri {
const float pi;
unsigned int selectedTri;
std::array<float, 4> selectedTriColor;
Color selectedTriColor;
float selectedTriBlinkTimer;
int inputWidth;
@ -97,8 +97,8 @@ namespace Tri {
void append_notification_text(const char *text);
public:
std::array<float, 4>& get_color();
std::array<float, 3>& get_bg_color();
Color& get_color();
Color& get_bg_color();
std::array<char, 256>* get_save_filename_buffer();
bool do_save();
@ -119,7 +119,7 @@ namespace Tri {
float get_pi() const;
std::array<float, 4>& get_selected_tri_color();
Color& get_selected_tri_color();
void close_selected_tri_mode();
private: