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) { inline void draw_color_picker(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_COLOR_P)) { if(state->get_flags().test(Tri::State::F_DISPLAY_COLOR_P)) {
GuiSetAlpha(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({4.0f, 4.0f, 242.0f, 292.0f}, "Tri Color Picker")) { if(!GuiWindowBox({4.0f, 4.0f, 242.0f, 292.0f}, "Tri Color Picker")) {
auto &colorArray = state->get_color(); 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( GuiColorPicker(
{8.0f, 32.0f, 206.0f, 240.0f}, {8.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
&color); &colorArray);
colorArray = color_to_float_color(color); if(GuiButton({8.0f, 274.0f, 234.0f, 16.0f}, "Close")) {
if(GuiButton({8.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_color_picker(); state->close_color_picker();
} }
} else { } else {
@ -155,14 +117,11 @@ namespace Tri {
GuiSetAlpha(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({250.0f, 4.0f, 242.0f, 292.0f}, "BG Color Picker")) { if(!GuiWindowBox({250.0f, 4.0f, 242.0f, 292.0f}, "BG Color Picker")) {
auto &colorArray = state->get_bg_color(); 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( GuiColorPicker(
{254.0f, 32.0f, 206.0f, 240.0f}, {254.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
&color); &colorArray);
colorArray = color_to_float3_color(color); if(GuiButton({254.0f, 274.0f, 234.0f, 16.0f}, "Close")) {
if(GuiButton({254.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_bg_color_picker(); state->close_bg_color_picker();
} }
} else { } else {
@ -291,13 +250,10 @@ namespace Tri {
GuiSetAlpha(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({500.0f, 4.0f, 242.0f, 292.0f}, "Edit Tri Color Picker")) { if(!GuiWindowBox({500.0f, 4.0f, 242.0f, 292.0f}, "Edit Tri Color Picker")) {
auto &colorArray = state->get_selected_tri_color(); 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( GuiColorPicker(
{504.0f, 32.0f, 206.0f, 240.0f}, {504.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
&color); &colorArray);
colorArray = color_to_float_color(color);
if(GuiButton({504.0f, 272.0f, 234.0f, 16.0f}, "Close")) { if(GuiButton({504.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_selected_tri_mode(); state->close_selected_tri_mode();
} }

View file

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

View file

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