@@ -546,7 +546,9 @@ json_t *appModuleBrowserToJson(); | |||||
void appModuleBrowserFromJson(json_t *rootJ); | void appModuleBrowserFromJson(json_t *rootJ); | ||||
/** Deprecated. Will be removed in v1 */ | |||||
json_t *colorToJson(NVGcolor color); | json_t *colorToJson(NVGcolor color); | ||||
/** Deprecated. Will be removed in v1 */ | |||||
NVGcolor jsonToColor(json_t *colorJ); | NVGcolor jsonToColor(json_t *colorJ); | ||||
@@ -1,13 +1,16 @@ | |||||
#pragma once | #pragma once | ||||
#include "util/math.hpp" | |||||
#include "util/common.hpp" | |||||
#include "nanovg.h" | #include "nanovg.h" | ||||
namespace rack { | namespace rack { | ||||
// TODO Make these non-inline in Rack v1 | |||||
inline NVGcolor colorClip(NVGcolor a) { | inline NVGcolor colorClip(NVGcolor a) { | ||||
for (int i = 0; i < 4; i++) | for (int i = 0; i < 4; i++) | ||||
a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f); | a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f); | ||||
@@ -59,5 +62,8 @@ inline NVGcolor colorAlpha(NVGcolor a, float alpha) { | |||||
return a; | return a; | ||||
} | } | ||||
NVGcolor colorFromHexString(std::string s); | |||||
std::string colorToHexString(NVGcolor c); | |||||
} // namespace rack | } // namespace rack |
@@ -46,6 +46,7 @@ void ModuleWidget::setPanel(std::shared_ptr<SVG> svg) { | |||||
// Remove old panel | // Remove old panel | ||||
if (panel) { | if (panel) { | ||||
removeChild(panel); | removeChild(panel); | ||||
delete panel; | |||||
panel = NULL; | panel = NULL; | ||||
} | } | ||||
@@ -144,14 +144,20 @@ Vec WireWidget::getInputPos() { | |||||
json_t *WireWidget::toJson() { | json_t *WireWidget::toJson() { | ||||
json_t *rootJ = json_object(); | 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; | return rootJ; | ||||
} | } | ||||
void WireWidget::fromJson(json_t *rootJ) { | void WireWidget::fromJson(json_t *rootJ) { | ||||
json_t *colorJ = json_object_get(rootJ, "color"); | 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) { | void WireWidget::draw(NVGcontext *vg) { | ||||
@@ -8,6 +8,7 @@ | |||||
#include "bridge.hpp" | #include "bridge.hpp" | ||||
#include "midi.hpp" | #include "midi.hpp" | ||||
#include "osdialog.h" | #include "osdialog.h" | ||||
#include "util/color.hpp" | |||||
#include <unistd.h> | #include <unistd.h> | ||||
@@ -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 |