chore(stdlib): update xod/color

This commit is contained in:
Kirill Shumilov
2020-08-21 18:27:28 +03:00
committed by Evgeny Kochetkov
parent 229e19a9a2
commit 5e9ff7939a
6 changed files with 116 additions and 149 deletions

View File

@@ -1,42 +1,37 @@
node {
float hueToRgb(float v1, float v2, float vH) {
if (vH < 0) vH += 1.0f;
struct State {};
if (vH > 1.0f) vH -= 1.0f;
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);
float hueToRgb(float v1, float v2, float vH) {
if (vH < 0) vH += 1.0f;
if ((2.0f * vH) < 1.0f) return v2;
if (vH > 1.0f) vH -= 1.0f;
if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);
return v1;
}
if ((2.0f * vH) < 1.0f) return v2;
void evaluate(Context ctx) {
float h = getValue<input_H>(ctx);
float s = getValue<input_S>(ctx);
float l = getValue<input_L>(ctx);
if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
uint8_t r, g, b;
return v1;
}
void evaluate(Context ctx) {
float h = getValue<input_H>(ctx);
float s = getValue<input_S>(ctx);
float l = getValue<input_L>(ctx);
uint8_t r, g, b;
if (s == 0) {
r = g = b = ceil(l * 255.0f);
} else {
float v2 = (l < 0.5) ? (l * (1.0f + s)) : ((l + s) - (l * s));
float v1 = 2.0f * l - v2;
r = round(255.0f * hueToRgb(v1, v2, h + (1.0f / 3.0f)));
g = round(255.0f * hueToRgb(v1, v2, h));
b = round(255.0f * hueToRgb(v1, v2, h - (1.0f / 3.0f)));
}
ValueType<output_OUT>::T obj = {r, g, b};
emitValue<output_OUT>(ctx, obj);
if (s == 0) {
r = g = b = ceil(l * 255.0f);
} else {
float v2 = (l < 0.5) ? (l * (1.0f + s)) : ((l + s) - (l * s));
float v1 = 2.0f * l - v2;
r = round(255.0f * hueToRgb(v1, v2, h + (1.0f / 3.0f)));
g = round(255.0f * hueToRgb(v1, v2, h));
b = round(255.0f * hueToRgb(v1, v2, h - (1.0f / 3.0f)));
}
typeof_OUT obj = {r, g, b};
emitValue<output_OUT>(ctx, obj);
}
}

View File

@@ -1,13 +1,7 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
ValueType<output_OUT>::T obj;
obj = { (uint8_t)getValue<input_R>(ctx), (uint8_t)getValue<input_G>(ctx), (uint8_t)getValue<input_B>(ctx) };
emitValue<output_OUT>(ctx, obj);
node {
void evaluate(Context ctx) {
typeof_OUT obj;
obj = { (uint8_t)getValue<input_R>(ctx), (uint8_t)getValue<input_G>(ctx), (uint8_t)getValue<input_B>(ctx) };
emitValue<output_OUT>(ctx, obj);
}
}

View File

@@ -1,30 +1,24 @@
struct State {
node {
char str[7] = { 0, 0, 0, 0, 0, 0, '\0' };
CStringView view;
State() : view(str) { }
};
CStringView view = CStringView(str);
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
char nibbleToChar(uint8_t nibble) {
return (nibble < 10)
? '0' + nibble
: 'A' + nibble - 10;
}
char nibbleToChar(uint8_t nibble) {
return (nibble < 10)
? '0' + nibble
: 'A' + nibble - 10;
}
void evaluate(Context ctx) {
auto state = getState(ctx);
auto color = getValue<input_IN>(ctx);
state->str[0] = nibbleToChar(color.r >> 4);
state->str[1] = nibbleToChar(color.r & 0x0F);
state->str[2] = nibbleToChar(color.g >> 4);
state->str[3] = nibbleToChar(color.g & 0x0F);
state->str[4] = nibbleToChar(color.b >> 4);
state->str[5] = nibbleToChar(color.b & 0x0F);
emitValue<output_OUT>(ctx, XString(&state->view));
void evaluate(Context ctx) {
auto color = getValue<input_IN>(ctx);
str[0] = nibbleToChar(color.r >> 4);
str[1] = nibbleToChar(color.r & 0x0F);
str[2] = nibbleToChar(color.g >> 4);
str[3] = nibbleToChar(color.g & 0x0F);
str[4] = nibbleToChar(color.b >> 4);
str[5] = nibbleToChar(color.b & 0x0F);
emitValue<output_OUT>(ctx, XString(&view));
}
}

View File

@@ -1,28 +1,25 @@
struct State {
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
};
node {
uint8_t _r = 0;
uint8_t _g = 0;
uint8_t _b = 0;
{{ GENERATED_CODE }}
bool isColorEqual(uint8_t r, uint8_t g, uint8_t b) {
return (
_r == r &&
_g == g &&
_b == b
);
}
bool isColorEqual(State* state, uint8_t r, uint8_t g, uint8_t b) {
return (
state->r == r &&
state->g == g &&
state->b == b
);
}
void evaluate(Context ctx) {
State* state = getState(ctx);
auto newColor = getValue<input_IN>(ctx);
if (!isSettingUp() && !isColorEqual(state, newColor.r, newColor.g, newColor.b));
emitValue<output_OUT>(ctx, 1);
state->r = newColor.r;
state->g = newColor.g;
state->b = newColor.b;
void evaluate(Context ctx) {
auto newColor = getValue<input_IN>(ctx);
if (!isSettingUp() && !isColorEqual(newColor.r, newColor.g, newColor.b));
emitValue<output_OUT>(ctx, 1);
_r = newColor.r;
_g = newColor.g;
_b = newColor.b;
}
}

View File

@@ -1,52 +1,45 @@
node {
void evaluate(Context ctx)
{
struct State {
};
auto color = getValue<input_IN>(ctx);
float r = color.r / 255.0f;
float g = color.g / 255.0f;
float b = color.b / 255.0f;
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
float _min = min(min(r, g), b);
float _max = max(max(r, g), b);
float delta = _max - _min;
void evaluate(Context ctx)
{
float l, s;
float hue;
auto color = getValue<input_IN>(ctx);
float r = color.r / 255.0f;
float g = color.g / 255.0f;
float b = color.b / 255.0f;
l = (_max + _min) / 2;
float _min = min(min(r, g), b);
float _max = max(max(r, g), b);
float delta = _max - _min;
float l, s;
float hue;
l = (_max + _min) / 2;
if (delta == 0) {
hue = 0;
s = 0;
}
else {
s = (l <= 0.5) ? (delta / (_max + _min)) : (delta / (2 - _max - _min));
if (r == _max) {
hue = ((g - b) / 6) / delta;
}
else if (g == _max) {
hue = (1.0f / 3) + ((b - r) / 6) / delta;
if (delta == 0) {
hue = 0;
s = 0;
}
else {
hue = (2.0f / 3) + ((r - g) / 6) / delta;
s = (l <= 0.5) ? (delta / (_max + _min)) : (delta / (2 - _max - _min));
if (r == _max) {
hue = ((g - b) / 6) / delta;
}
else if (g == _max) {
hue = (1.0f / 3) + ((b - r) / 6) / delta;
}
else {
hue = (2.0f / 3) + ((r - g) / 6) / delta;
}
if (hue < 0)
hue += 1;
if (hue > 1)
hue -= 1;
}
if (hue < 0)
hue += 1;
if (hue > 1)
hue -= 1;
emitValue<output_H>(ctx, hue);
emitValue<output_S>(ctx, s);
emitValue<output_L>(ctx, l);
}
emitValue<output_H>(ctx, hue);
emitValue<output_S>(ctx, s);
emitValue<output_L>(ctx, l);
}

View File

@@ -1,14 +1,8 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto color = getValue<input_IN>(ctx);
emitValue<output_R>(ctx, color.r);
emitValue<output_G>(ctx, color.g);
emitValue<output_B>(ctx, color.b);
node {
void evaluate(Context ctx) {
auto color = getValue<input_IN>(ctx);
emitValue<output_R>(ctx, color.r);
emitValue<output_G>(ctx, color.g);
emitValue<output_B>(ctx, color.b);
}
}