refactor: modal-gate helper + data-driven colormap table
Two extensibility easy-wins, both behavior-preserving: - UiModalOpen() centralizes the "an overlay owns input" check that was copy-pasted as `!showFileBrowser && !showAbout` across 6 input gates. New overlays now update one place. - Colormaps become a COLORMAPS[] table (name + function) indexed by enum. GetColormapColor and the sidebar both read it, so adding a colormap is one enum value + one Cmap* fn + one row — and the sidebar name can no longer drift from the enum (deleted the parallel colormapNames[] array). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+71
-40
@@ -41,48 +41,79 @@ float MeasureTextScaled(const char* text, float baseSize)
|
||||
}
|
||||
|
||||
// ===== Colormaps =====
|
||||
// Each colormap maps t in [0,1] to a Color. To add a colormap: add an enum
|
||||
// value in spectrogram_types.h, write a Cmap* function, and add one row to
|
||||
// COLORMAPS[] below — the sidebar names and lookups follow automatically.
|
||||
typedef Color (*ColormapFn)(float t);
|
||||
|
||||
static Color CmapGrays(float t)
|
||||
{
|
||||
unsigned char v = (unsigned char)(t * 255);
|
||||
return (Color){ v, v, v, 255 };
|
||||
}
|
||||
|
||||
static Color CmapInferno(float t)
|
||||
{
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f;
|
||||
if (t < 0.25f) { t = t / 0.25f; r = 0.0f + t * 0.5f; g = 0.0f; b = 0.0f + t * 0.3f; }
|
||||
else if (t < 0.5f) { t = (t - 0.25f) / 0.25f; r = 0.5f + t * 0.5f; g = 0.0f + t * 0.3f; b = 0.3f + t * 0.4f; }
|
||||
else if (t < 0.75f) { t = (t - 0.5f) / 0.25f; r = 1.0f; g = 0.3f + t * 0.5f; b = 0.7f + t * 0.2f; }
|
||||
else { t = (t - 0.75f) / 0.25f; r = 1.0f; g = 0.8f + t * 0.2f; b = 0.9f + t * 0.1f; }
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
|
||||
static Color CmapViridis(float t)
|
||||
{
|
||||
float r, g, b;
|
||||
if (t < 0.25f) { t = t / 0.25f; r = 0.27f + t * 0.13f; g = 0.00f + t * 0.33f; b = 0.33f + t * 0.27f; }
|
||||
else if (t < 0.5f) { t = (t - 0.25f) / 0.25f; r = 0.40f + t * 0.16f; g = 0.33f + t * 0.29f; b = 0.60f - t * 0.20f; }
|
||||
else if (t < 0.75f) { t = (t - 0.5f) / 0.25f; r = 0.56f + t * 0.24f; g = 0.62f + t * 0.23f; b = 0.40f - t * 0.20f; }
|
||||
else { t = (t - 0.75f) / 0.25f; r = 0.80f + t * 0.17f; g = 0.85f + t * 0.12f; b = 0.20f - t * 0.15f; }
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
|
||||
static Color CmapPlasma(float t)
|
||||
{
|
||||
float r = 0.05f + t * 0.9f;
|
||||
float g = 0.0f + t * 0.6f + (t > 0.5f ? (t - 0.5f) * 0.4f : 0.0f);
|
||||
float b = 0.6f - t * 0.5f;
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
|
||||
static Color CmapHot(float t)
|
||||
{
|
||||
float r = Clamp(t * 3.0f, 0.0f, 1.0f);
|
||||
float g = Clamp((t - 0.33f) * 3.0f, 0.0f, 1.0f);
|
||||
float b = Clamp((t - 0.66f) * 3.0f, 0.0f, 1.0f);
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
|
||||
static Color CmapCool(float t)
|
||||
{
|
||||
return (Color){ (unsigned char)(t * 255), (unsigned char)((1.0f - t) * 255), 255, 255 };
|
||||
}
|
||||
|
||||
typedef struct { const char* name; ColormapFn fn; } ColormapDef;
|
||||
|
||||
static const ColormapDef COLORMAPS[COLORMAP_COUNT] = {
|
||||
[COLORMAP_GRAYS] = { "Grays", CmapGrays },
|
||||
[COLORMAP_INFERNO] = { "Inferno", CmapInferno },
|
||||
[COLORMAP_VIRIDIS] = { "Viridis", CmapViridis },
|
||||
[COLORMAP_PLASMA] = { "Plasma", CmapPlasma },
|
||||
[COLORMAP_HOT] = { "Hot", CmapHot },
|
||||
[COLORMAP_COOL] = { "Cool", CmapCool },
|
||||
};
|
||||
|
||||
static Color GetColormapColor(float t, ColormapType type)
|
||||
{
|
||||
t = Clamp(t, 0.0f, 1.0f);
|
||||
|
||||
switch (type) {
|
||||
case COLORMAP_GRAYS: {
|
||||
unsigned char v = (unsigned char)(t * 255);
|
||||
return (Color){ v, v, v, 255 };
|
||||
}
|
||||
case COLORMAP_INFERNO: {
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f;
|
||||
if (t < 0.25f) { t = t / 0.25f; r = 0.0f + t * 0.5f; g = 0.0f; b = 0.0f + t * 0.3f; }
|
||||
else if (t < 0.5f) { t = (t - 0.25f) / 0.25f; r = 0.5f + t * 0.5f; g = 0.0f + t * 0.3f; b = 0.3f + t * 0.4f; }
|
||||
else if (t < 0.75f) { t = (t - 0.5f) / 0.25f; r = 1.0f; g = 0.3f + t * 0.5f; b = 0.7f + t * 0.2f; }
|
||||
else { t = (t - 0.75f) / 0.25f; r = 1.0f; g = 0.8f + t * 0.2f; b = 0.9f + t * 0.1f; }
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
case COLORMAP_VIRIDIS: {
|
||||
float r, g, b;
|
||||
if (t < 0.25f) { t = t / 0.25f; r = 0.27f + t * 0.13f; g = 0.00f + t * 0.33f; b = 0.33f + t * 0.27f; }
|
||||
else if (t < 0.5f) { t = (t - 0.25f) / 0.25f; r = 0.40f + t * 0.16f; g = 0.33f + t * 0.29f; b = 0.60f - t * 0.20f; }
|
||||
else if (t < 0.75f) { t = (t - 0.5f) / 0.25f; r = 0.56f + t * 0.24f; g = 0.62f + t * 0.23f; b = 0.40f - t * 0.20f; }
|
||||
else { t = (t - 0.75f) / 0.25f; r = 0.80f + t * 0.17f; g = 0.85f + t * 0.12f; b = 0.20f - t * 0.15f; }
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
case COLORMAP_PLASMA: {
|
||||
float r = 0.05f + t * 0.9f;
|
||||
float g = 0.0f + t * 0.6f + (t > 0.5f ? (t - 0.5f) * 0.4f : 0.0f);
|
||||
float b = 0.6f - t * 0.5f;
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
case COLORMAP_HOT: {
|
||||
float r = Clamp(t * 3.0f, 0.0f, 1.0f);
|
||||
float g = Clamp((t - 0.33f) * 3.0f, 0.0f, 1.0f);
|
||||
float b = Clamp((t - 0.66f) * 3.0f, 0.0f, 1.0f);
|
||||
return (Color){ (unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255), 255 };
|
||||
}
|
||||
case COLORMAP_COOL: {
|
||||
return (Color){ (unsigned char)(t * 255), (unsigned char)((1.0f - t) * 255), 255, 255 };
|
||||
}
|
||||
default: return GRAY;
|
||||
}
|
||||
if (type < 0 || type >= COLORMAP_COUNT) return GRAY;
|
||||
return COLORMAPS[type].fn(Clamp(t, 0.0f, 1.0f));
|
||||
}
|
||||
|
||||
const char* ColormapName(ColormapType type)
|
||||
{
|
||||
if (type < 0 || type >= COLORMAP_COUNT) return "?";
|
||||
return COLORMAPS[type].name;
|
||||
}
|
||||
|
||||
void GenerateColormapTexture(void)
|
||||
|
||||
Reference in New Issue
Block a user