Skip to content

Commit

Permalink
Merge pull request #21 from Unreal-Dan/kurt/refine-randomizer
Browse files Browse the repository at this point in the history
kurt/refine-randomizer
  • Loading branch information
Unreal-Dan authored Jan 17, 2024
2 parents 4140b2c + ed34f75 commit af450db
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 65 deletions.
45 changes: 4 additions & 41 deletions Colorset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,12 @@ void Colorset::removeColor(uint8_t index)
m_palette[--m_numColors].clear();
}

// create a set of truely random colors
void Colorset::randomize(Random &ctx, uint8_t numColors)
{
clear();
if (!numColors) {
numColors = ctx.next8(2, NUM_COLOR_SLOTS);
}
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
for (uint8_t i = 0; i < numColors; ++i) {
// do not put the next8() calls inside arguments, the order functions in
// arguments are called is undefined behaviour and it's important that
// these are called in the right order otherwise different platforms will
// behave differently when performing this randomization
uint8_t sat = ctx.next8();
uint8_t hue = ctx.next8();
addColorWithValueStyle(ctx, hue, sat, valStyle, numColors, i);
}
}
uint8_t current_color_mode = Colorset::THEORY;

void Colorset::randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode)
void Colorset::randomizeColors(Random &ctx, uint8_t numColors)
{
current_color_mode = (current_color_mode + 1) % COLOR_MODE_COUNT;
ColorMode mode = (ColorMode)current_color_mode;
clear();
if (!numColors) {
numColors = ctx.next8(mode == MONOCHROMATIC ? 2 : 1, 9);
Expand Down Expand Up @@ -229,28 +214,6 @@ void Colorset::randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode)
}
}

void Colorset::randomizeColors2(Random &ctx, ColorMode2 mode)
{
clear();
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);
}
}

void Colorset::adjustBrightness(uint8_t fadeby)
{
for (uint8_t i = 0; i < m_numColors; ++i) {
Expand Down
23 changes: 3 additions & 20 deletions Colorset.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,15 @@ class Colorset
ValueStyle valStyle, uint8_t numColors, uint8_t colorPos);
void removeColor(uint8_t index);

// randomize a colorset with a specific number of colors with
// various different randomization techniques
void randomize(Random &ctx, uint8_t numColors = 0);

// function to randomize the colors with various different modes of randomization
enum ColorMode {
THEORY,
MONOCHROMATIC,
EVENLY_SPACED
EVENLY_SPACED,
COLOR_MODE_COUNT
};
void randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode);

// 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) { randomizeColors(ctx, 1, EVENLY_SPACED); }
void randomizeComplimentary(Random &ctx) { randomizeColors(ctx, 2, EVENLY_SPACED); }
void randomizeTriadic(Random &ctx) { randomizeColors(ctx, 3, EVENLY_SPACED); }
void randomizeSquare(Random &ctx) { randomizeColors(ctx, 4, EVENLY_SPACED); }
void randomizePentadic(Random &ctx) { randomizeColors(ctx, 5, EVENLY_SPACED); }
void randomizeRainbow(Random &ctx) { randomizeColors(ctx, 8, EVENLY_SPACED); }
void randomizeColors(Random &ctx, uint8_t numColors);

// fade all of the colors in the set
void adjustBrightness(uint8_t fadeby);
Expand Down
5 changes: 3 additions & 2 deletions Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,8 @@ void Helios::handle_state_randomize()
Random ctx(seed);
Colorset &cur_set = pat.colorset();
uint8_t num_cols = (ctx.next8() + 1) % NUM_COLOR_SLOTS;
cur_set.randomizeColors(ctx, num_cols, Colorset::THEORY);

cur_set.randomizeColors(ctx, num_cols);
Patterns::make_pattern((PatternID)(ctx.next8() % PATTERN_COUNT), pat);
pat.init();
}
Expand All @@ -774,7 +775,7 @@ void Helios::save_global_flags()

void Helios::show_selection(RGBColor color)
{
// only show seletion while pressing the button
// only show selection while pressing the button
if (!Button::isPressed()) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Patterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static const uint32_t color_codes0[] = {RGB_RED, RGB_ORANGE, RGB_YELLOW, RGB_TUR
static const uint32_t color_codes1[] = {RGB_RED, RGB_CORAL_ORANGE_SAT_MEDIUM, RGB_ORANGE, RGB_YELLOW_SAT_LOW};
static const uint32_t color_codes2[] = {RGB_PURPLE_SAT_MEDIUM, RGB_RED_BRI_LOWEST, RGB_MAGENTA_BRI_LOWEST, RGB_BLUE_BRI_LOWEST};
static const uint32_t color_codes3[] = {RGB_MAGENTA, RGB_YELLOW, RGB_TURQUOISE, RGB_PINK_SAT_LOW, RGB_RED, RGB_YELLOW};
static const uint32_t color_codes4[] = {RGB_WHITE_BRI_LOWEST, RGB_ROYAL_BLUE_BRI_LOW, RGB_TURQUOISE, RGB_ROYAL_BLUE_BRI_LOW, RGB_MAGENTA_BRI_LOWEST};
static const uint32_t color_codes4[] = {RGB_MAGENTA_BRI_LOWEST, RGB_ROYAL_BLUE_BRI_LOW, RGB_TURQUOISE, RGB_ROYAL_BLUE_BRI_LOW, RGB_MAGENTA_BRI_LOWEST, RGB_OFF};
static const uint32_t color_codes5[] = {RGB_RED, RGB_HOT_PINK, RGB_ROYAL_BLUE, RGB_BLUE, RGB_GREEN, RGB_YELLOW};

// Define Colorset configurations for each slot
Expand All @@ -26,7 +26,7 @@ static const default_colorset default_colorsets[] = {
{ 4, color_codes1 }, // 1 Sauna
{ 4, color_codes2 }, // 2 UltraViolet
{ 6, color_codes3 }, // 3 Space Carnival
{ 5, color_codes4 }, // 4 Ice Blade
{ 6, color_codes4 }, // 4 Ice Blade
{ 6, color_codes5 }, // 5 Rainbow Glitter
};

Expand Down

0 comments on commit af450db

Please sign in to comment.