Skip to content

Commit fc46881

Browse files
authored
Merge pull request #2 from ModMaker101/dev
fixing a bug with the fade lol
2 parents 99f5a8a + 7318dda commit fc46881

File tree

3 files changed

+77
-93
lines changed

3 files changed

+77
-93
lines changed

KeyLighting/CPUImageProcessor.cs

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CPUImageProcessor : IDisposable
2424
private OpenRGB.NET.Color[] resultBuffer;
2525
private bool hasPreviousFrame = false;
2626

27-
private double fadeSpeed;
27+
private double fadeSpeed;
2828

2929
public CPUImageProcessor(LightingConfig config)
3030
{
@@ -107,12 +107,12 @@ public OpenRGB.NET.Color[] ProcessImage(Bitmap image, int targetWidth, int targe
107107
}
108108
else
109109
{
110-
111110
ProcessColumnsWithEffects(targetWidth, brightness, vibrance, contrast, darkThreshold, darkFactor);
112111

113-
if (hasPreviousFrame)
112+
// Apply fading only if fade speed is less than 1.0
113+
if (hasPreviousFrame && fadeSpeed < 1.0)
114114
{
115-
ApplyFading(targetWidth, lastFrameWasSolid ? 0.95 : fadeSpeed);
115+
ApplyFading(targetWidth, fadeSpeed);
116116
}
117117
}
118118

@@ -134,39 +134,7 @@ public OpenRGB.NET.Color[] ProcessImage(Bitmap image, int targetWidth, int targe
134134
}
135135
}
136136

137-
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
138-
private void ProcessSolidColor(byte r, byte g, byte b, int width, double brightness, double vibrance, double contrast, int darkThreshold, double darkFactor)
139-
{
140-
141-
OpenRGB.NET.Color processedColor = FastApplyEffects(r, g, b, brightness, vibrance, contrast, darkThreshold, darkFactor);
142137

143-
bool needsFade = hasPreviousFrame && !(lastFrameWasSolid &&
144-
lastSolidR == processedColor.R &&
145-
lastSolidG == processedColor.G &&
146-
lastSolidB == processedColor.B);
147-
148-
if (needsFade)
149-
{
150-
151-
double fadeFactor = 0.95;
152-
153-
Parallel.For(0, width, i => {
154-
resultBuffer[i] = FastBlendColors(previousFrame[i], processedColor, fadeFactor);
155-
});
156-
}
157-
else
158-
{
159-
160-
for (int i = 0; i < width; i++)
161-
{
162-
resultBuffer[i] = processedColor;
163-
}
164-
}
165-
166-
lastSolidR = processedColor.R;
167-
lastSolidG = processedColor.G;
168-
lastSolidB = processedColor.B;
169-
}
170138

171139
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172140
private bool AreSettingsCached(double brightness, double contrast)
@@ -189,13 +157,13 @@ private bool IsSolidColorFrame()
189157
if (rawColors.Length < 2) return true;
190158

191159
OpenRGB.NET.Color first = rawColors[0];
192-
const int tolerance = 5;
160+
const int tolerance = 5;
193161

194162
int[] samplePoints = { 0, rawColors.Length / 3, rawColors.Length / 2, (rawColors.Length * 2) / 3, rawColors.Length - 1 };
195163

196164
foreach (int i in samplePoints)
197165
{
198-
if (i == 0) continue;
166+
if (i == 0) continue;
199167

200168
if (Math.Abs(first.R - rawColors[i].R) > tolerance ||
201169
Math.Abs(first.G - rawColors[i].G) > tolerance ||
@@ -211,6 +179,9 @@ private bool IsSolidColorFrame()
211179
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
212180
private void ApplyFading(int width, double fadeFactor)
213181
{
182+
// This method should only be called when fadeSpeed < 1.0
183+
// Simply use the provided fade factor without any brightness-based adjustments
184+
214185
Parallel.For(0, width, i => {
215186
resultBuffer[i] = FastBlendColors(previousFrame[i], resultBuffer[i], fadeFactor);
216187
});
@@ -219,17 +190,58 @@ private void ApplyFading(int width, double fadeFactor)
219190
[MethodImpl(MethodImplOptions.AggressiveInlining)]
220191
private OpenRGB.NET.Color FastBlendColors(OpenRGB.NET.Color color1, OpenRGB.NET.Color color2, double factor)
221192
{
222-
223193
factor = Math.Clamp(factor, 0.0, 1.0);
224194
double inverseFactor = 1.0 - factor;
225195

196+
// Process each channel with adaptive blending
226197
byte r = (byte)(color1.R * inverseFactor + color2.R * factor);
227198
byte g = (byte)(color1.G * inverseFactor + color2.G * factor);
228199
byte b = (byte)(color1.B * inverseFactor + color2.B * factor);
229200

230201
return new OpenRGB.NET.Color(r, g, b);
231202
}
232203

204+
// Also modify ProcessSolidColor method to handle brightness transitions better
205+
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
206+
private void ProcessSolidColor(byte r, byte g, byte b, int width, double brightness, double vibrance, double contrast, int darkThreshold, double darkFactor)
207+
{
208+
OpenRGB.NET.Color processedColor = FastApplyEffects(r, g, b, brightness, vibrance, contrast, darkThreshold, darkFactor);
209+
210+
// Check if we need to apply fading
211+
bool needsFade = hasPreviousFrame &&
212+
fadeSpeed < 1.0 && // Only fade if fade speed is less than 1.0
213+
!(lastFrameWasSolid &&
214+
lastSolidR == processedColor.R &&
215+
lastSolidG == processedColor.G &&
216+
lastSolidB == processedColor.B);
217+
218+
if (needsFade)
219+
{
220+
// Calculate brightness values for current and previous frame
221+
int prevBrightness = lastSolidR + lastSolidG + lastSolidB;
222+
int newBrightness = processedColor.R + processedColor.G + processedColor.B;
223+
224+
// Determine if we're brightening or darkening
225+
double fadeFactor = fadeSpeed; // Use the configured fade speed
226+
227+
// Apply transition
228+
Parallel.For(0, width, i => {
229+
resultBuffer[i] = FastBlendColors(previousFrame[i], processedColor, fadeFactor);
230+
});
231+
}
232+
else
233+
{
234+
for (int i = 0; i < width; i++)
235+
{
236+
resultBuffer[i] = processedColor;
237+
}
238+
}
239+
240+
lastSolidR = processedColor.R;
241+
lastSolidG = processedColor.G;
242+
lastSolidB = processedColor.B;
243+
}
244+
233245
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
234246
private void ExtractColumns(int stride, int width, int height, int bytesPerPixel)
235247
{

KeyLighting/Program.cs

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Program
2626
static DateTime lastUpdate = DateTime.MinValue;
2727
static DateTime lastDebugImageSave = DateTime.MinValue;
2828

29-
static float[] fadeProgress;
29+
// Remove fade progress tracking
30+
// static float[] fadeProgress;
3031
static ORGBColor[] targetColors;
3132

3233
const int MIN_CAPTURE_INTERVAL_MS = 16;
@@ -74,7 +75,7 @@ static void Main(string[] args)
7475

7576
prevColors = new ORGBColor[ledCount];
7677
ledColorsBuffer = new ORGBColor[ledCount];
77-
fadeProgress = new float[ledCount];
78+
// Removed fadeProgress
7879
targetColors = new ORGBColor[ledCount];
7980

8081
var processor = new CPUImageProcessor(config);
@@ -216,6 +217,8 @@ static void ProcessFrame(ScreenCapturer capturer, CPUImageProcessor processor, O
216217
[MethodImpl(MethodImplOptions.AggressiveInlining)]
217218
static void UpdateLedColors(ORGBColor[] columnColors, LightingConfig config, int ledCount)
218219
{
220+
// Check if we want instant transitions (fadeSpeed at or very near 1.0)
221+
bool instantTransition = config.FadeFactor >= 0.99;
219222

220223
var wasdEnabled = config.WASDEnabled;
221224
var wasdKeys = wasdEnabled ? config.WASDKeys : Array.Empty<int>();
@@ -230,77 +233,46 @@ static void UpdateLedColors(ORGBColor[] columnColors, LightingConfig config, int
230233
}
231234

232235
int columnLength = columnColors.Length;
233-
float deltaTime = 1.0f / 60.0f;
234236

235237
for (int i = 0; i < ledCount; i++)
236238
{
237239
if (wasdEnabled && Array.IndexOf(wasdKeys, i) >= 0)
238240
{
239-
240-
continue;
241-
}
242-
else
243-
{
244-
245-
int columnIndex = Math.Min(i, columnLength - 1);
246-
targetColors[i] = columnColors[columnIndex];
247-
}
248-
}
249-
250-
for (int i = 0; i < ledCount; i++)
251-
{
252-
if (wasdEnabled && Array.IndexOf(wasdKeys, i) >= 0)
253-
{
254-
241+
// Handle WASD keys with special color
255242
ledColorsBuffer[i] = new ORGBColor(wasdR, wasdG, wasdB);
256-
prevColors[i] = ledColorsBuffer[i];
257-
fadeProgress[i] = 1.0f;
258243
}
259244
else
260245
{
246+
// Apply column colors to the keyboard
247+
int columnIndex = Math.Min(i, columnLength - 1);
261248

262-
ORGBColor prev = prevColors[i];
263-
ORGBColor target = targetColors[i];
264-
265-
bool colorChanged =
266-
Math.Abs(prev.R - target.R) > 3 ||
267-
Math.Abs(prev.G - target.G) > 3 ||
268-
Math.Abs(prev.B - target.B) > 3;
269-
270-
if (colorChanged && fadeProgress[i] >= 1.0f)
249+
if (instantTransition)
271250
{
272-
273-
fadeProgress[i] = 0.0f;
251+
// With instantTransition, directly apply the column color
252+
ledColorsBuffer[i] = columnColors[columnIndex];
274253
}
254+
else
255+
{
256+
// For backward compatibility, keep some very minimal smoothing
257+
ORGBColor prev = prevColors[i];
258+
ORGBColor target = columnColors[columnIndex];
275259

276-
fadeProgress[i] = (float)Math.Min(fadeProgress[i] + config.FadeFactor * deltaTime, 1.0f);
277-
278-
float t = EaseInOutCubic(fadeProgress[i]);
279-
280-
byte r = (byte)Math.Round(prev.R * (1 - t) + target.R * t);
281-
byte g = (byte)Math.Round(prev.G * (1 - t) + target.G * t);
282-
byte b = (byte)Math.Round(prev.B * (1 - t) + target.B * t);
260+
// Simple lerp with very high weight toward target color
261+
float t = 0.8f; // High value for quick transition but not instant
283262

284-
ledColorsBuffer[i] = new ORGBColor(r, g, b);
263+
byte r = (byte)Math.Round(prev.R * (1 - t) + target.R * t);
264+
byte g = (byte)Math.Round(prev.G * (1 - t) + target.G * t);
265+
byte b = (byte)Math.Round(prev.B * (1 - t) + target.B * t);
285266

286-
if (fadeProgress[i] >= 1.0f)
287-
{
288-
prevColors[i] = target;
289-
}
290-
else
291-
{
292-
prevColors[i] = ledColorsBuffer[i];
267+
ledColorsBuffer[i] = new ORGBColor(r, g, b);
293268
}
269+
270+
// Store current color for next frame
271+
prevColors[i] = ledColorsBuffer[i];
294272
}
295273
}
296274
}
297275

298-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
299-
static float EaseInOutCubic(float t)
300-
{
301-
return t < 0.5 ? 4 * t * t * t : 1 - (float)Math.Pow(-2 * t + 2, 3) / 2;
302-
}
303-
304276
static void SaveDebugImages(Bitmap frame, ORGBColor[] columnColors, LightingConfig config)
305277
{
306278
try

KeyLighting/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"brightnessMultiplier": 1.8,
77
"vibranceFactor": 1,
88
"contrastPower": 1.8,
9-
"fadeFactor": 0.9,
9+
"fadeFactor": 0.3,
1010
"darkenThreshold": 65,
1111
"darkenFactor": 0.4,
1212
"wasdEnabled": false,

0 commit comments

Comments
 (0)