MCPcopy Create free account
hub / github.com/TombEngine/TombEngine / GetDominantColor

Method GetDominantColor

TombEngine/Specific/Video/Video.cpp:50–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48 }
49
50 Color VideoHandler::GetDominantColor() const
51 {
52 if (_player == nullptr || _frameBuffer.size() == 0)
53 return Color(0.0f, 0.0f, 0.0f);
54
55 unsigned long long accR = 0;
56 unsigned long long accG = 0;
57 unsigned long long accB = 0;
58
59 int pixelCount = 0;
60 int step = 8;
61
62 for (int y = 0; y < _size.y; y += step)
63 {
64 for (int x = 0; x < _size.x; x += step)
65 {
66 int index = (y * (x * 4)) + (x * 4);
67
68 unsigned char b = _frameBuffer[index];
69 unsigned char g = _frameBuffer[index + 1];
70 unsigned char r = _frameBuffer[index + 2];
71
72 accR += r;
73 accG += g;
74 accB += b;
75 pixelCount++;
76 }
77 }
78
79 if (pixelCount == 0)
80 return Color(0.0f, 0.0f, 0.0f);
81
82 unsigned char avgR = unsigned char(accR / pixelCount);
83 unsigned char avgG = unsigned char(accG / pixelCount);
84 unsigned char avgB = unsigned char(accB / pixelCount);
85
86 auto result = Vector3((float)avgR / 255.0f, (float)avgG / 255.0f, (float)avgB / 255.0f);
87
88 float luma = Luma(result);
89 if (luma < 0.3f && luma > 0.0f)
90 {
91 float boostFactor = 0.3f / luma;
92 result *= boostFactor;
93 result.Clamp(Vector3::Zero, Vector3::One);
94 }
95
96 if (luma < 0.5f)
97 {
98 float desaturationFactor = (0.4f - luma) / 0.4f;
99 result.x = (result.x * (1.0f - desaturationFactor)) + (luma * desaturationFactor);
100 result.y = (result.y * (1.0f - desaturationFactor)) + (luma * desaturationFactor);
101 result.z = (result.z * (1.0f - desaturationFactor)) + (luma * desaturationFactor);
102 }
103
104 return Color(result);
105 }
106
107 bool VideoHandler::GetSilent() const

Callers 1

GetVideoDominantColorFunction · 0.80

Calls 4

LumaFunction · 0.85
Vector3Function · 0.50
sizeMethod · 0.45
ClampMethod · 0.45

Tested by

no test coverage detected