Skip to content

Commit

Permalink
Optimize visual palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Greedysky committed Dec 14, 2023
1 parent 559c655 commit 6cd743f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
github: [Greedysky]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
Expand Down
52 changes: 26 additions & 26 deletions src/spek-palette.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

#define GRADIENT_TABLE_SIZE 256

static bool globalTableInit = false;
static uint32_t globalTableGolors[GRADIENT_TABLE_SIZE];
static QVector<QColor> globalGolors = { QColor(0, 0, 0),
QColor(0, 32, 100),
QColor(0, 120, 160),
QColor(128, 255, 120),
QColor(255, 255, 0),
QColor(255, 128, 0),
QColor(255, 0, 0)
};
static bool __globalTableInit__ = false;
static uint32_t __globalTableGolors__[GRADIENT_TABLE_SIZE];
static QVector<QColor> __globalGolors__ = { QColor(0, 0, 0),
QColor(0, 32, 100),
QColor(0, 120, 160),
QColor(128, 255, 120),
QColor(255, 255, 0),
QColor(255, 128, 0),
QColor(255, 0, 0)
};

static void createGradientTable()
{
int numbers = 6;
constexpr int numbers = 6;
for (int i = 0; i < GRADIENT_TABLE_SIZE; ++i) {
double position = (double)i/GRADIENT_TABLE_SIZE;
/* if position > 1 then we have repetition of colors it maybe useful */
Expand All @@ -35,17 +35,17 @@ static void createGradientTable()
const int n = (int)m; // integer of m
const double f = m - n; // fraction of m

globalTableGolors[i] = 0xFF0000;
__globalTableGolors__[i] = 0xFF0000;
if (n < numbers) {
globalTableGolors[i] = ((uint32_t)((globalGolors[n].red()) + f * ((globalGolors[n+1].red()) - (globalGolors[n].red()))) & 0xFF) << 16 |
((uint32_t)((globalGolors[n].green()) + f * ((globalGolors[n+1].green()) - (globalGolors[n].green()))) & 0xFF) << 8 |
((uint32_t)((globalGolors[n].blue()) + f * ((globalGolors[n+1].blue()) - (globalGolors[n].blue()))) & 0xFF) << 0;
__globalTableGolors__[i] = ((uint32_t)((__globalGolors__[n].red()) + f * ((__globalGolors__[n+1].red()) - (__globalGolors__[n].red()))) & 0xFF) << 16 |
((uint32_t)((__globalGolors__[n].green()) + f * ((__globalGolors__[n+1].green()) - (__globalGolors__[n].green()))) & 0xFF) << 8 |
((uint32_t)((__globalGolors__[n].blue()) + f * ((__globalGolors__[n+1].blue()) - (__globalGolors__[n].blue()))) & 0xFF) << 0;
} else if (n == numbers) {
globalTableGolors[i] = ((uint32_t)(globalGolors[n].red()) & 0xFF) << 16 |
((uint32_t)(globalGolors[n].green()) & 0xFF) << 8 |
((uint32_t)(globalGolors[n].blue()) & 0xFF) << 0;
__globalTableGolors__[i] = ((uint32_t)(__globalGolors__[n].red()) & 0xFF) << 16 |
((uint32_t)(__globalGolors__[n].green()) & 0xFF) << 8 |
((uint32_t)(__globalGolors__[n].blue()) & 0xFF) << 0;
} else {
globalTableGolors[i] = 0xFFFFFF;
__globalTableGolors__[i] = 0xFFFFFF;
}
}
}
Expand Down Expand Up @@ -92,18 +92,18 @@ static uint32_t spectrum(double level)
return (rr << 16) + (gg << 8) + bb;
}

static uint32_t rainbow(double level)
static uint32_t perceptual(double level)
{
if (!globalTableInit) {
if (!__globalTableInit__) {
createGradientTable();
globalTableInit = true;
__globalTableInit__ = true;
}

const int index = qBound(0, int(level * GRADIENT_TABLE_SIZE), GRADIENT_TABLE_SIZE - 1);
return globalTableGolors[index];
return __globalTableGolors__[index];
}

static uint32_t perceptual(double level)
static uint32_t rainbow(double level)
{
float R, G, B, I, H, S, key = 1.5;
level *= 256;
Expand Down Expand Up @@ -168,10 +168,10 @@ uint32_t spek_palette(Palette palette, double level)
switch (palette) {
case PALETTE_SPECTRUM:
return spectrum(level);
case PALETTE_RAINBOW:
return rainbow(level);
case PALETTE_PERCEPTUAL:
return perceptual(level);
case PALETTE_RAINBOW:
return rainbow(level);
case PALETTE_SOX:
return sox(level);
case PALETTE_MONO:
Expand Down
8 changes: 4 additions & 4 deletions src/spek-palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

#include <stdint.h>

typedef enum palette {
enum Palette {
PALETTE_SPECTRUM,
PALETTE_RAINBOW,
PALETTE_PERCEPTUAL,
PALETTE_RAINBOW,
PALETTE_SOX,
PALETTE_MONO,
PALETTE_COUNT,
PALETTE_DEFAULT = PALETTE_RAINBOW,
}Palette;
PALETTE_DEFAULT = PALETTE_PERCEPTUAL,
};

uint32_t spek_palette(Palette palette, double level);

Expand Down
4 changes: 2 additions & 2 deletions src/spek-spectrogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ void SpekSpectrogram::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_P:
if (event->modifiers() == Qt::NoModifier) { // 'p'
palette = (enum palette) ((this->palette + 1) % PALETTE_COUNT);
palette = (Palette) ((this->palette + 1) % PALETTE_COUNT);
create_palette();
} else if (event->modifiers() == Qt::ShiftModifier) { // 'P'
palette = (enum palette) ((this->palette - 1 + PALETTE_COUNT) % PALETTE_COUNT);
palette = (Palette) ((this->palette - 1 + PALETTE_COUNT) % PALETTE_COUNT);
create_palette();
}
break;
Expand Down

0 comments on commit 6cd743f

Please sign in to comment.