Browse Source

Use hex strings for colors in patch file instead of object

tags/v0.6.1
Andrew Belt 6 years ago
parent
commit
64458ce60f
6 changed files with 48 additions and 4 deletions
  1. +2
    -0
      include/app.hpp
  2. +7
    -1
      include/util/color.hpp
  3. +1
    -0
      src/app/ModuleWidget.cpp
  4. +9
    -3
      src/app/WireWidget.cpp
  5. +1
    -0
      src/main.cpp
  6. +28
    -0
      src/util/color.cpp

+ 2
- 0
include/app.hpp View File

@@ -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);




+ 7
- 1
include/util/color.hpp View File

@@ -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

+ 1
- 0
src/app/ModuleWidget.cpp View File

@@ -46,6 +46,7 @@ void ModuleWidget::setPanel(std::shared_ptr<SVG> svg) {
// Remove old panel
if (panel) {
removeChild(panel);
delete panel;
panel = NULL;
}



+ 9
- 3
src/app/WireWidget.cpp View File

@@ -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) {


+ 1
- 0
src/main.cpp View File

@@ -8,6 +8,7 @@
#include "bridge.hpp"
#include "midi.hpp"
#include "osdialog.h"
#include "util/color.hpp"

#include <unistd.h>



+ 28
- 0
src/util/color.cpp View File

@@ -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

Loading…
Cancel
Save