Skip to content

Commit 38cbd17

Browse files
committed
Favorite color WIP
1 parent 4f83086 commit 38cbd17

5 files changed

Lines changed: 82 additions & 3 deletions

File tree

lib/libkiwi/core/kiwiColor.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
11
#include <libkiwi.h>
22

3+
#include <cmath>
4+
35
namespace kiwi {
46

57
/**
68
* @brief Converts RGB color to YUV format
79
*/
8-
Color Color::Yuv() const {
10+
Color Color::ToYuv() const {
911
u8 y = 0.257f * r + 0.504f * g + 0.098f * b + 16.0f;
1012
u8 u = -0.148f * r - 0.291f * g + 0.439f * b + 128.0f;
1113
u8 v = 0.439f * r - 0.368f * g - 0.071f * b + 128.0f;
1214
return Color(y, u, v, a);
1315
}
1416

17+
/**
18+
* @brief Creates RGB color from HSV format
19+
*
20+
* @param h Hue [0, 1]
21+
* @param s Saturation [0, 1]
22+
* @param v Value [0, 1]
23+
*/
24+
Color Color::FromHsv(f32 h, f32 s, f32 v) {
25+
K_ASSERT(h >= 0.0f && h <= 1.0f);
26+
K_ASSERT(s >= 0.0f && s <= 1.0f);
27+
K_ASSERT(v >= 0.0f && v <= 1.0f);
28+
29+
f32 r = 0.0f;
30+
f32 g = 0.0f;
31+
f32 b = 0.0f;
32+
33+
s32 i = static_cast<s32>(h * 6.0f);
34+
f32 f = h * 6.0f - i;
35+
36+
f32 p = v * (1.0f - s);
37+
f32 q = v * (1.0f - f * s);
38+
f32 t = v * (1.0f - (1.0f - f) * s);
39+
40+
// clang-format off
41+
switch (i % 6) {
42+
case 0: { r = v, g = t, b = p; break; }
43+
case 1: { r = q, g = v, b = p; break; }
44+
case 2: { r = p, g = v, b = t; break; }
45+
case 3: { r = p, g = q, b = v; break; }
46+
case 4: { r = t, g = p, b = v; break; }
47+
case 5: { r = v, g = p, b = q; break; }
48+
}
49+
// clang-format on
50+
51+
r *= 255;
52+
g *= 255;
53+
b *= 255;
54+
55+
// clang-format off
56+
return Color(static_cast<u8>(r),
57+
static_cast<u8>(g),
58+
static_cast<u8>(b),
59+
255);
60+
// clang-format on
61+
}
62+
1563
} // namespace kiwi

lib/libkiwi/core/kiwiColor.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ class Color : public nw4r::ut::Color {
7171
/**
7272
* @brief Converts RGB color to YUV format
7373
*/
74-
Color Yuv() const;
74+
Color ToYuv() const;
75+
76+
/**
77+
* @brief Converts HSV color to RGB format
78+
*
79+
* @param h Hue [0, 1]
80+
* @param s Saturation [0, 1]
81+
* @param v Value [0, 1]
82+
*/
83+
static Color FromHsv(f32 h, f32 s, f32 v);
7584

7685
public:
7786
/**

lib/libkiwi/debug/kiwiNw4rDirectPrint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void Nw4rDirectPrint::DrawString(s32 x, s32 y, const char* pMsg, ...) const {
203203
*/
204204
void Nw4rDirectPrint::SetColor(Color rgb) {
205205
// Framebuffer uses YUV format, so we convert the color
206-
mBufferColor = rgb.Yuv();
206+
mBufferColor = rgb.ToYuv();
207207
}
208208

209209
/**

lib/libkiwi/util/kiwiRandom.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ class Random {
112112
return NextF32() * max;
113113
}
114114

115+
/**
116+
* @brief Get random float (lower+upper bound)
117+
*
118+
* @param min Lower bound (inclusive)
119+
* @param max Upper bound (exclusive)
120+
*/
121+
f32 NextF32(f32 min, f32 max) {
122+
K_ASSERT(min < max);
123+
return min + NextF32(max - min);
124+
}
125+
115126
/**
116127
* @brief Roll random chance
117128
*

src/hooks/Cmn/cosmetics_Cmn.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ RPSysScene::ETime InterceptIslandTime(RPSysScene* pScene) {
7676
}
7777
KM_CALL(0x8026a56c, InterceptIslandTime);
7878

79+
/**
80+
* @brief Overrides the Mii favorite color based on the randomizer settings
81+
*/
82+
u32 InterceptFavoriteColor() {
83+
return kiwi::Color::FromHsv(kiwi::Random().NextF32(1.0f),
84+
kiwi::Random().NextF32(0.85f, 0.9f),
85+
kiwi::Random().NextF32(0.5f, 0.9f));
86+
}
87+
88+
KM_BRANCH(0x80102a20, InterceptFavoriteColor);
89+
7990
} // namespace Cosmetic
8091
} // namespace Cmn
8192
} // namespace AP

0 commit comments

Comments
 (0)