Skip to content

Commit

Permalink
Merge branch 'master' into daniel/core/dynamic_led_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Aug 7, 2023
2 parents 91fadf8 + 7dcd79e commit 5bad8c4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 103 deletions.
2 changes: 1 addition & 1 deletion VortexEngine/src/Colors/ColorTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ RGBColor hsv_to_rgb_generic(const HSVColor &rhs)
}

region = rhs.hue / 43;
remainder = ((rhs.hue - (region * 43)) * 6) % 256;
remainder = ((rhs.hue - (region * 43)) * 6);

// extraneous casts to uint16_t are to prevent overflow
p = (uint8_t)(((uint16_t)(rhs.val) * (255 - rhs.sat)) >> 8);
Expand Down
114 changes: 33 additions & 81 deletions VortexEngine/src/Colors/Colorset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,43 +232,17 @@ void Colorset::randomize(Random &ctx, uint8_t numColors)
}
}

// create a set according to the rules of color theory
void Colorset::randomizeColorTheory(Random &ctx, uint8_t numColors)
void Colorset::randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode)
{
clear();
if (!numColors) {
numColors = ctx.next8(1, 9);
numColors = ctx.next8(mode == MONOCHROMATIC ? 2 : 1, 9);
}
uint8_t randomizedHue = ctx.next8();
uint8_t colorGap = 0;
if (numColors > 1) colorGap = ctx.next8(16, 256 / (numColors - 1));
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
// the doubleStyle decides if some colors are added to the set twice
uint8_t doubleStyle = 0;
if (numColors <= 7) {
doubleStyle = (ctx.next8(0, 1));
}
if (numColors <= 4) {
doubleStyle = (ctx.next8(0, 2));
if (mode == THEORY && numColors > 1) {
colorGap = ctx.next8(16, 256 / (numColors - 1));
}
for (uint8_t i = 0; i < numColors; i++) {
uint8_t nextHue = (randomizedHue + (i * colorGap)) % 256;
addColorWithValueStyle(ctx, nextHue, 255, valStyle, numColors, i);
// double all colors or only first color
if (doubleStyle == 2 || (doubleStyle == 1 && !i)) {
addColorWithValueStyle(ctx, nextHue, 255, valStyle, numColors, i);
}
}
}

// create a set of colors that share a single hue
void Colorset::randomizeMonochromatic(Random &ctx, uint8_t numColors)
{
clear();
if (!numColors) {
numColors = ctx.next8(2, 9);
}
uint8_t randomizedHue = ctx.next8();
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
// the doubleStyle decides if some colors are added to the set twice
uint8_t doubleStyle = 0;
Expand All @@ -279,65 +253,43 @@ void Colorset::randomizeMonochromatic(Random &ctx, uint8_t numColors)
doubleStyle = (ctx.next8(0, 2));
}
for (uint8_t i = 0; i < numColors; i++) {
uint8_t decrement = 255 - (i * (256 / numColors));
addColorWithValueStyle(ctx, randomizedHue, decrement, valStyle, numColors, i);
uint8_t hueToUse;
uint8_t valueToUse = 255;
if (mode == THEORY) {
hueToUse = (randomizedHue + (i * colorGap));
} else if (mode == MONOCHROMATIC) {
hueToUse = randomizedHue;
valueToUse = 255 - (i * (256 / numColors));
} else { // EVENLY_SPACED
hueToUse = (randomizedHue + (256 / numColors) * i);
}
addColorWithValueStyle(ctx, hueToUse, valueToUse, valStyle, numColors, i);
// double all colors or only first color
if (doubleStyle == 2 || (doubleStyle == 1 && !i)) {
addColorWithValueStyle(ctx, randomizedHue, decrement, valStyle, numColors, i);
addColorWithValueStyle(ctx, hueToUse, valueToUse, valStyle, numColors, i);
}
}
}

// create a set of 5 colors with 2 pairs of opposing colors with the same spacing from the central color
void Colorset::randomizeDoubleSplitComplimentary(Random &ctx)
void Colorset::randomizeColors2(Random &ctx, ColorMode2 mode)
{
clear();
uint8_t rHue = ctx.next8();
uint8_t splitGap = ctx.next8(1, 64);
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
addColorWithValueStyle(ctx, (rHue + splitGap + 128) % 256, 255, valStyle, 5, 0);
addColorWithValueStyle(ctx, (rHue - splitGap) % 256, 255, valStyle, 5, 1);
addColorWithValueStyle(ctx, rHue, 255, valStyle, 5, 2);
addColorWithValueStyle(ctx, (rHue + splitGap) % 256, 255, valStyle, 5, 3);
addColorWithValueStyle(ctx, (rHue - splitGap + 128) % 256, 255, valStyle, 5, 4);
}

// create a set of 2 pairs of oposing colors
void Colorset::randomizeTetradic(Random &ctx)
{
clear();
uint8_t randomizedHue = ctx.next8();
uint8_t randomizedHue2 = ctx.next8();
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
addColorWithValueStyle(ctx, randomizedHue, 255, valStyle, 4, 0);
addColorWithValueStyle(ctx, randomizedHue2, 255, valStyle, 4, 1);
addColorWithValueStyle(ctx, (randomizedHue + 128) % 256, 255, valStyle, 4, 2);
addColorWithValueStyle(ctx, (randomizedHue2 + 128) % 256, 255, valStyle, 4, 3);
}

void Colorset::randomizeEvenlySpaced(Random &ctx, uint8_t spaces)
{
clear();
if (!spaces) {
spaces = ctx.next8(1, 9);
}
uint8_t randomizedHue = ctx.next8();
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
// the doubleStyle decides if some colors are added to the set twice
uint8_t doubleStyle = 0;
if (spaces <= 7) {
doubleStyle = (ctx.next8(0, 1));
}
if (spaces <= 4) {
doubleStyle = (ctx.next8(0, 2));
}
for (uint8_t i = 0; i < spaces; i++) {
uint8_t nextHue = (randomizedHue + (256 / spaces) * i) % 256;
addColorWithValueStyle(ctx, nextHue, 255, valStyle, spaces, i);
// double all colors or only first color
if (doubleStyle == 2 || (doubleStyle == 1 && !i)) {
addColorWithValueStyle(ctx, nextHue, 255, valStyle, spaces, i);
}
uint8_t primaryHue = ctx.next8();
if (mode == DOUBLE_SPLIT_COMPLIMENTARY) {
uint8_t splitGap = ctx.next8(1, 64);
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
addColorWithValueStyle(ctx, (primaryHue + splitGap + 128), 255, valStyle, 5, 0);
addColorWithValueStyle(ctx, (primaryHue - splitGap), 255, valStyle, 5, 1);
addColorWithValueStyle(ctx, primaryHue, 255, valStyle, 5, 2);
addColorWithValueStyle(ctx, (primaryHue + splitGap), 255, valStyle, 5, 3);
addColorWithValueStyle(ctx, (primaryHue - splitGap + 128), 255, valStyle, 5, 4);
} else if (mode == TETRADIC) {
uint8_t secondaryHue = ctx.next8();
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
addColorWithValueStyle(ctx, primaryHue, 255, valStyle, 4, 0);
addColorWithValueStyle(ctx, secondaryHue, 255, valStyle, 4, 1);
addColorWithValueStyle(ctx, (primaryHue + 128), 255, valStyle, 4, 2);
addColorWithValueStyle(ctx, (secondaryHue + 128), 255, valStyle, 4, 3);
}
}

Expand Down
32 changes: 19 additions & 13 deletions VortexEngine/src/Colors/Colorset.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,29 @@ class Colorset
// randomize a colorset with a specific number of colors with
// various different randomization techniques
void randomize(Random &ctx, uint8_t numColors = 0);
void randomizeColorTheory(Random &ctx, uint8_t numColors = 0);
void randomizeMonochromatic(Random &ctx, uint8_t numColors = 0);

// these randomizers have a set amount of colors and don't take any arguments
void randomizeDoubleSplitComplimentary(Random &ctx);
void randomizeTetradic(Random &ctx);
// function to randomize the colors with various different modes of randomization
enum ColorMode {
THEORY,
MONOCHROMATIC,
EVENLY_SPACED
};
void randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode);

// randomize a colorset with N evenly spaced colors
void randomizeEvenlySpaced(Random &ctx, uint8_t spaces = 0);
// similar function but with some different modes
enum ColorMode2 {
DOUBLE_SPLIT_COMPLIMENTARY,
TETRADIC
};
void randomizeColors2(Random &ctx, ColorMode2 mode);

// wrappers for various spacings
void randomizeSolid(Random &ctx) { randomizeEvenlySpaced(ctx, 1); }
void randomizeComplimentary(Random &ctx) { randomizeEvenlySpaced(ctx, 2); }
void randomizeTriadic(Random &ctx) { randomizeEvenlySpaced(ctx, 3); }
void randomizeSquare(Random &ctx) { randomizeEvenlySpaced(ctx, 4); }
void randomizePentadic(Random &ctx) { randomizeEvenlySpaced(ctx, 5); }
void randomizeRainbow(Random &ctx) { randomizeEvenlySpaced(ctx, 8); }
void randomizeSolid(Random &ctx) { randomizeColors(ctx, 1, Colorset::ColorMode::EVENLY_SPACED); }
void randomizeComplimentary(Random &ctx) { randomizeColors(ctx, 2, Colorset::ColorMode::EVENLY_SPACED); }
void randomizeTriadic(Random &ctx) { randomizeColors(ctx, 3, Colorset::ColorMode::EVENLY_SPACED); }
void randomizeSquare(Random &ctx) { randomizeColors(ctx, 4, Colorset::ColorMode::EVENLY_SPACED); }
void randomizePentadic(Random &ctx) { randomizeColors(ctx, 5, Colorset::ColorMode::EVENLY_SPACED); }
void randomizeRainbow(Random &ctx) { randomizeColors(ctx, 8, Colorset::ColorMode::EVENLY_SPACED); }

// fade all of the colors in the set
void adjustBrightness(uint8_t fadeby);
Expand Down
14 changes: 7 additions & 7 deletions VortexEngine/src/Menus/MenuList/Randomizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,28 @@ Colorset Randomizer::rollColorset(Random &ctx)
randomSet.randomize(ctx);
break;
case 1:
randomSet.randomizeColorTheory(ctx);
randomSet.randomizeColors(ctx, 0, Colorset::ColorMode::THEORY);
break;
case 2:
randomSet.randomizeMonochromatic(ctx);
randomSet.randomizeColors(ctx, 0, Colorset::ColorMode::MONOCHROMATIC);
break;
case 3:
randomSet.randomizeDoubleSplitComplimentary(ctx);
randomSet.randomizeColors2(ctx, Colorset::ColorMode2::DOUBLE_SPLIT_COMPLIMENTARY);
break;
case 4:
randomSet.randomizeTetradic(ctx);
randomSet.randomizeColors2(ctx, Colorset::ColorMode2::TETRADIC);
break;
case 5:
randomSet.randomize(ctx, 1);
break;
case 6:
randomSet.randomizeEvenlySpaced(ctx);
randomSet.randomizeColors(ctx, 0, Colorset::ColorMode::EVENLY_SPACED);
break;
case 7:
randomSet.randomizeEvenlySpaced(ctx, 2);
randomSet.randomizeColors(ctx, 2, Colorset::ColorMode::EVENLY_SPACED);
break;
case 8:
randomSet.randomizeEvenlySpaced(ctx, 3);
randomSet.randomizeColors(ctx, 3, Colorset::ColorMode::EVENLY_SPACED);
break;
}

Expand Down
5 changes: 4 additions & 1 deletion VortexEngine/src/Patterns/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ uint8_t Pattern::getArg(uint8_t index) const

uint8_t &Pattern::argRef(uint8_t index)
{
return *((uint8_t *)this + m_argList[index % m_numArgs]);
if (index >= m_numArgs) {
index = 0;
}
return *((uint8_t *)this + m_argList[index]);
}

void Pattern::setArgs(const PatternArgs &args)
Expand Down

0 comments on commit 5bad8c4

Please sign in to comment.