Player12
An open source football management simulator
Loading...
Searching...
No Matches
match_kit_colors.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <imgui.h>
12
13#include <cmath>
14#include <cstdint>
15
16#include "global/types.h"
17
18struct ClubIdentity;
19
22{
23 ImU32 shirt = 0;
24 ImU32 trim = 0;
25 ImU32 shorts = 0;
26 ImU32 socks = 0;
27};
28
37
40{
41 ImU32 primary = 0;
42 ImU32 secondary = 0;
43};
44
54MatchKits chooseMatchKits(TeamID homeTeam, TeamID awayTeam);
55
57MatchKits chooseMatchKits(TeamID homeTeam, TeamID awayTeam,
58 const ClubColours* homeColours,
59 const ClubColours* awayColours);
60
66
68constexpr ImU32 kitColorFromRgb(std::uint32_t rgb)
69{
70 return IM_COL32((rgb >> 16U) & 0xFFU, (rgb >> 8U) & 0xFFU, rgb & 0xFFU, 255);
71}
72
74float kitColorDistance(ImU32 first, ImU32 second);
75
77inline constexpr float KIT_CLASH_DISTANCE = 190.0f;
78
84ImU32 kitNumberColor(const KitColors& kit);
85
87ImU32 goalkeeperGloveColor(const KitColors& kit);
88
90inline ImU32 shadeColor(ImU32 color, float factor)
91{
92 const auto scale = [factor](ImU32 value, int shift)
93 {
94 const float scaled =
95 static_cast<float>((value >> shift) & 0xFFU) * factor + 0.5f;
96 float bounded = scaled;
97 if (scaled < 0.0f)
98 bounded = 0.0f;
99 else if (scaled > 255.0f)
100 bounded = 255.0f;
101 return static_cast<ImU32>(bounded) << shift;
102 };
103 return scale(color, IM_COL32_R_SHIFT) | scale(color, IM_COL32_G_SHIFT) |
104 scale(color, IM_COL32_B_SHIFT) | (color & IM_COL32_A_MASK);
105}
106
108inline ImU32 mixColor(ImU32 first, ImU32 second, float t)
109{
110 const auto blend = [t](ImU32 a, ImU32 b, int shift)
111 {
112 const auto from = static_cast<float>((a >> shift) & 0xFFU);
113 const auto to = static_cast<float>((b >> shift) & 0xFFU);
114 return static_cast<ImU32>(std::lerp(from, to, t) + 0.5f) << shift;
115 };
116 return blend(first, second, IM_COL32_R_SHIFT) |
117 blend(first, second, IM_COL32_G_SHIFT) |
118 blend(first, second, IM_COL32_B_SHIFT) |
119 blend(first, second, IM_COL32_A_SHIFT);
120}
121
126constexpr ImU32 mixColor256(ImU32 first, ImU32 second, std::uint32_t t256)
127{
128 const std::uint32_t keep = 256U - t256;
129 const std::uint32_t evenChannels =
130 (((first & 0x00FF00FFU) * keep + (second & 0x00FF00FFU) * t256) >> 8U) &
131 0x00FF00FFU;
132 const std::uint32_t oddChannels = ((((first >> 8U) & 0x00FF00FFU) * keep +
133 ((second >> 8U) & 0x00FF00FFU) * t256) >>
134 8U) &
135 0x00FF00FFU;
136 return evenChannels | (oddChannels << 8U);
137}
138
139constexpr ImU32 shadeColor256(ImU32 color, std::uint32_t factor256)
140{
141 const auto channel = [&](int shift)
142 {
143 const std::uint32_t value = (((color >> shift) & 0xFFU) * factor256) >> 8U;
144 return (value > 255U ? 255U : value) << shift;
145 };
146 return channel(IM_COL32_R_SHIFT) | channel(IM_COL32_G_SHIFT) |
147 channel(IM_COL32_B_SHIFT) | (color & IM_COL32_A_MASK);
148}
149
151constexpr ImU32 withAlpha(ImU32 color, std::uint8_t alpha)
152{
153 return (color & ~IM_COL32_A_MASK) |
154 (static_cast<ImU32>(alpha) << IM_COL32_A_SHIFT);
155}
156
158constexpr std::uint32_t cosmeticHash(std::uint32_t value)
159{
160 value ^= value >> 16;
161 value *= 0x7feb352dU;
162 value ^= value >> 15;
163 value *= 0x846ca68bU;
164 value ^= value >> 16;
165 return value;
166}
float kitColorDistance(ImU32 first, ImU32 second)
Definition match_kit_colors.cpp:115
constexpr ImU32 withAlpha(ImU32 color, std::uint8_t alpha)
Definition match_kit_colors.h:151
constexpr float KIT_CLASH_DISTANCE
Definition match_kit_colors.h:77
MatchKits chooseMatchKits(TeamID homeTeam, TeamID awayTeam)
Definition match_kit_colors.cpp:161
constexpr ImU32 shadeColor256(ImU32 color, std::uint32_t factor256)
Definition match_kit_colors.h:139
const ClubIdentity * findClubIdentity(TeamID team)
Definition match_kit_colors.cpp:108
constexpr std::uint32_t cosmeticHash(std::uint32_t value)
Definition match_kit_colors.h:158
ImU32 mixColor(ImU32 first, ImU32 second, float t)
Definition match_kit_colors.h:108
constexpr ImU32 kitColorFromRgb(std::uint32_t rgb)
Definition match_kit_colors.h:68
ImU32 goalkeeperGloveColor(const KitColors &kit)
Definition match_kit_colors.cpp:142
constexpr ImU32 mixColor256(ImU32 first, ImU32 second, std::uint32_t t256)
Definition match_kit_colors.h:126
ImU32 shadeColor(ImU32 color, float factor)
Definition match_kit_colors.h:90
ImU32 kitNumberColor(const KitColors &kit)
Definition match_kit_colors.cpp:131
Definition match_kit_colors.h:40
ImU32 primary
Definition match_kit_colors.h:41
ImU32 secondary
Definition match_kit_colors.h:42
Descriptive club data read from the team files of the data pack.
Definition datagenerator.h:29
Definition match_kit_colors.h:22
ImU32 shorts
Definition match_kit_colors.h:25
ImU32 trim
Definition match_kit_colors.h:24
ImU32 socks
Definition match_kit_colors.h:26
ImU32 shirt
Definition match_kit_colors.h:23
Definition match_kit_colors.h:31
KitColors away
Definition match_kit_colors.h:33
KitColors homeGoalkeeper
Definition match_kit_colors.h:34
KitColors home
Definition match_kit_colors.h:32
KitColors awayGoalkeeper
Definition match_kit_colors.h:35
uint16_t TeamID
Type alias for Team identifiers.
Definition types.h:18