From 7dcd79eb8972ef27526c95b985ff18953973ef71 Mon Sep 17 00:00:00 2001 From: Unreal-Dan <72595612+Unreal-Dan@users.noreply.github.com> Date: Sun, 6 Aug 2023 19:49:42 -0700 Subject: [PATCH] First step of refactor (#90) refactor colorset and randomizer --- VortexEngine/src/Colors/ColorTypes.cpp | 2 +- VortexEngine/src/Colors/Colorset.cpp | 114 +++++------------- VortexEngine/src/Colors/Colorset.h | 32 +++-- .../src/Menus/MenuList/Randomizer.cpp | 14 +-- VortexEngine/src/Patterns/Pattern.cpp | 5 +- 5 files changed, 64 insertions(+), 103 deletions(-) diff --git a/VortexEngine/src/Colors/ColorTypes.cpp b/VortexEngine/src/Colors/ColorTypes.cpp index f18ac2b323..3a7b2e8d81 100644 --- a/VortexEngine/src/Colors/ColorTypes.cpp +++ b/VortexEngine/src/Colors/ColorTypes.cpp @@ -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); diff --git a/VortexEngine/src/Colors/Colorset.cpp b/VortexEngine/src/Colors/Colorset.cpp index ed9521b59f..b90d95ea35 100644 --- a/VortexEngine/src/Colors/Colorset.cpp +++ b/VortexEngine/src/Colors/Colorset.cpp @@ -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; @@ -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); } } diff --git a/VortexEngine/src/Colors/Colorset.h b/VortexEngine/src/Colors/Colorset.h index 67910de2d3..099b4ab177 100644 --- a/VortexEngine/src/Colors/Colorset.h +++ b/VortexEngine/src/Colors/Colorset.h @@ -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); diff --git a/VortexEngine/src/Menus/MenuList/Randomizer.cpp b/VortexEngine/src/Menus/MenuList/Randomizer.cpp index 1516ba661e..50e77116af 100644 --- a/VortexEngine/src/Menus/MenuList/Randomizer.cpp +++ b/VortexEngine/src/Menus/MenuList/Randomizer.cpp @@ -157,28 +157,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; } diff --git a/VortexEngine/src/Patterns/Pattern.cpp b/VortexEngine/src/Patterns/Pattern.cpp index 1b271aa818..7b2ea29b88 100644 --- a/VortexEngine/src/Patterns/Pattern.cpp +++ b/VortexEngine/src/Patterns/Pattern.cpp @@ -105,7 +105,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)