From f8bf481809eb9df33a1698e4d813241cc323ac5a Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 21 May 2018 17:11:07 -0400 Subject: [PATCH] Use hex strings for colors in patch file instead of object --- include/app.hpp | 2 ++ include/util/color.hpp | 8 +++++++- src/app/ModuleWidget.cpp | 1 + src/app/WireWidget.cpp | 12 +++++++++--- src/main.cpp | 1 + src/util/color.cpp | 28 ++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/util/color.cpp diff --git a/include/app.hpp b/include/app.hpp index 24b10df8..419459f9 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -546,7 +546,9 @@ json_t *appModuleBrowserToJson(); void appModuleBrowserFromJson(json_t *rootJ); +/** Deprecated. Will be removed in v1 */ json_t *colorToJson(NVGcolor color); +/** Deprecated. Will be removed in v1 */ NVGcolor jsonToColor(json_t *colorJ); diff --git a/include/util/color.hpp b/include/util/color.hpp index 25134425..9d6b7b8a 100644 --- a/include/util/color.hpp +++ b/include/util/color.hpp @@ -1,13 +1,16 @@ #pragma once -#include "util/math.hpp" +#include "util/common.hpp" #include "nanovg.h" namespace rack { +// TODO Make these non-inline in Rack v1 + + inline NVGcolor colorClip(NVGcolor a) { for (int i = 0; i < 4; i++) a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f); @@ -59,5 +62,8 @@ inline NVGcolor colorAlpha(NVGcolor a, float alpha) { return a; } +NVGcolor colorFromHexString(std::string s); +std::string colorToHexString(NVGcolor c); + } // namespace rack diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 19b2f24a..3eb3efce 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -46,6 +46,7 @@ void ModuleWidget::setPanel(std::shared_ptr svg) { // Remove old panel if (panel) { removeChild(panel); + delete panel; panel = NULL; } diff --git a/src/app/WireWidget.cpp b/src/app/WireWidget.cpp index 6292daf4..1b65c688 100644 --- a/src/app/WireWidget.cpp +++ b/src/app/WireWidget.cpp @@ -144,14 +144,20 @@ Vec WireWidget::getInputPos() { json_t *WireWidget::toJson() { json_t *rootJ = json_object(); - json_object_set_new(rootJ, "color", colorToJson(color)); + std::string s = colorToHexString(color); + json_object_set_new(rootJ, "color", json_string(s.c_str())); return rootJ; } void WireWidget::fromJson(json_t *rootJ) { json_t *colorJ = json_object_get(rootJ, "color"); - if (colorJ) - color = jsonToColor(colorJ); + if (colorJ) { + // Legacy v0.6.0 and earlier + if (json_is_object(colorJ)) + color = jsonToColor(colorJ); + else + color = colorFromHexString(json_string_value(colorJ)); + } } void WireWidget::draw(NVGcontext *vg) { diff --git a/src/main.cpp b/src/main.cpp index cd506769..fa5e28b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "bridge.hpp" #include "midi.hpp" #include "osdialog.h" +#include "util/color.hpp" #include diff --git a/src/util/color.cpp b/src/util/color.cpp new file mode 100644 index 00000000..f322b181 --- /dev/null +++ b/src/util/color.cpp @@ -0,0 +1,28 @@ +#include "util/color.hpp" + + +namespace rack { + + +NVGcolor colorFromHexString(std::string s) { + uint8_t r = 0; + uint8_t g = 0; + uint8_t b = 0; + uint8_t a = 255; + sscanf(s.c_str(), "#%2hhx%2hhx%2hhx%2hhx", &r, &g, &b, &a); + return nvgRGBA(r, g, b, a); +} + +std::string colorToHexString(NVGcolor c) { + uint8_t r = roundf(c.r * 255); + uint8_t g = roundf(c.g * 255); + uint8_t b = roundf(c.b * 255); + uint8_t a = roundf(c.a * 255); + if (a == 255) + return stringf("#%02x%02x%02x", r, g, b); + else + return stringf("#%02x%02x%02x%02x", r, g, b, a); +} + + +} // namespace rack