Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 42b6cb8

Browse files
pretty random color for chapters
1 parent b4fbe44 commit 42b6cb8

5 files changed

Lines changed: 621 additions & 0 deletions

File tree

OFS-lib/OFS_Util.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define SINFL_IMPLEMENTATION
3434
#include "sinfl.h"
3535

36+
#define RND_IMPLEMENTATION
37+
#include "rnd.h"
38+
3639
char Util::FormatBuffer[4096];
3740

3841
static void SanitizeString(std::string& str) noexcept
@@ -358,3 +361,30 @@ std::filesystem::path Util::FfmpegPath() noexcept
358361
return ffmpegPath;
359362
#endif
360363
}
364+
365+
static rnd_pcg_t pcg;
366+
void Util::InitRandom() noexcept
367+
{
368+
time_t t = time(0);
369+
rnd_pcg_seed(&pcg, t);
370+
}
371+
372+
float Util::NextFloat() noexcept
373+
{
374+
return rnd_pcg_nextf(&pcg);
375+
}
376+
377+
uint32_t Util::RandomColor(float s, float v, float alpha) noexcept
378+
{
379+
// This is cool :^)
380+
// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
381+
constexpr float goldenRatioConjugate = 0.618033988749895f;
382+
static float H = NextFloat();
383+
384+
H += goldenRatioConjugate;
385+
H = SDL_fmodf(H, 1.f);
386+
387+
ImColor color;
388+
color.SetHSV(H, s, v, alpha);
389+
return ImGui::ColorConvertFloat4ToU32(color);
390+
}

OFS-lib/OFS_Util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ class Util {
511511
return (*pf)(decltype(args)(args)...);
512512
};
513513
}
514+
515+
static void InitRandom() noexcept;
516+
static float NextFloat() noexcept;
517+
static uint32_t RandomColor(float s, float v, float alpha = 1.f) noexcept;
514518
};
515519

516520
#define FMT(fmt, ...) Util::Format(fmt, __VA_ARGS__)

OFS-lib/state/states/ChapterState.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Chapter* ChapterState::AddChapter(float time, float duration) noexcept
8989
Chapter newChapter = {0};
9090
newChapter.startTime = startTime;
9191
newChapter.endTime = endTime;
92+
newChapter.color = Util::RandomColor(0.65f, 0.90f);
9293

9394
if(chapters.empty())
9495
{

0 commit comments

Comments
 (0)