1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-13 09:08:34 +00:00

Added color-blending

This commit is contained in:
Darth Affe 2015-09-22 23:12:19 +02:00
parent 0a47642c38
commit 22c23af4fd
4 changed files with 99 additions and 25 deletions

View File

@ -88,6 +88,7 @@
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" /> <Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
<Compile Include="Devices\Mouse\CorsairMouse.cs" /> <Compile Include="Devices\Mouse\CorsairMouse.cs" />
<Compile Include="Devices\Keyboard\Extensions\KeyGroupExtension.cs" /> <Compile Include="Devices\Keyboard\Extensions\KeyGroupExtension.cs" />
<Compile Include="Helper\ColorHelper.cs" />
<Compile Include="Helper\RectangleHelper.cs" /> <Compile Include="Helper\RectangleHelper.cs" />
<Compile Include="Native\_CorsairDeviceInfo.cs" /> <Compile Include="Native\_CorsairDeviceInfo.cs" />
<Compile Include="Native\_CorsairLedColor.cs" /> <Compile Include="Native\_CorsairLedColor.cs" />

View File

@ -1,4 +1,5 @@
using System.Drawing; using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Devices.Generic namespace CUE.NET.Devices.Generic
{ {
@ -19,7 +20,7 @@ namespace CUE.NET.Devices.Generic
{ {
if (!IsLocked) if (!IsLocked)
{ {
RequestedColor = value; RequestedColor = RequestedColor.Blend(value);
IsUpdated = true; IsUpdated = true;
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -48,10 +47,10 @@ namespace SimpleDevTest
keyboard[CorsairKeyboardKeyId.G].Led.Color = Color.Green; keyboard[CorsairKeyboardKeyId.G].Led.Color = Color.Green;
keyboard['B'].Led.Color = Color.Blue; keyboard['B'].Led.Color = Color.Blue;
// Lock the 'r', 'g', 'b' keys. We want them to stay like this forever // Lock the 'r', 'g', 'b' keys. We want them to stay like this forever (commented since it looks quite stupid later, but feel free tu uncomment this)
keyboard['R'].Led.IsLocked = true; //keyboard['R'].Led.IsLocked = true;
keyboard['G'].Led.IsLocked = true; //keyboard['G'].Led.IsLocked = true;
keyboard['B'].Led.IsLocked = true; //keyboard['B'].Led.IsLocked = true;
// Ink the letters of 'white' white // Ink the letters of 'white' white
ListKeyGroup whiteGroup = new ListKeyGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E) ListKeyGroup whiteGroup = new ListKeyGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E)
@ -73,11 +72,11 @@ namespace SimpleDevTest
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Now let us move a orange point random over the keyboard // Now let us move some points random over the keyboard
// Something like this could become some sort of effect // Something like this could become some sort of effect
// Initialize needed stuff // Initialize needed stuff
const float SPEED = 8f; // mm/tick const float SPEED = 4f; // mm/tick
Random random = new Random(); Random random = new Random();
// Remove all the groups we created above to clear the keyboard // Remove all the groups we created above to clear the keyboard
@ -96,30 +95,54 @@ namespace SimpleDevTest
Thread.Sleep(200); Thread.Sleep(200);
} }
// Set keyboard 'background' to something fancy // Set keyboard 'background' to something neutral
keyboard.Brush = new SolidColorBrush(Color.DarkSlateBlue); keyboard.Brush = new SolidColorBrush(Color.Black);
// Spawn our point (rectangle since circles are too hard to calculate :p) in the top-left corner (right over G1 or on ESC depending on your keyboard) // Define how many points we have
RectangleF point = new RectangleF(keyboard.KeyboardRectangle.X, keyboard.KeyboardRectangle.Y, 40, 40); const int NUM_POINTS = 6;
// Create a new KeyGroup to represent our point on the keyboard // The points we want to draw (rectangle since circles are too hard to calculate :p)
RectangleKeyGroup pointGroup = new RectangleKeyGroup(keyboard, point, 0.1f) RectangleF[] points = new RectangleF[NUM_POINTS];
{ Brush = new SolidColorBrush(Color.Orange) };
// KeyGroups which represents our point on the keyboard
RectangleKeyGroup[] pointGroups = new RectangleKeyGroup[NUM_POINTS];
// Target of our movement // Target of our movement
PointF target = new PointF(point.X, point.Y); PointF[] targets = new PointF[NUM_POINTS];
// Initialize all the stuff
for (int i = 0; i < NUM_POINTS; i++)
{
// Spawn our point in the top-left corner (right over G1 or on ESC depending on your keyboard)
points[i] = new RectangleF(keyboard.KeyboardRectangle.X, keyboard.KeyboardRectangle.Y, 60, 60);
pointGroups[i] = new RectangleKeyGroup(keyboard, points[i], 0.1f) { Brush = new SolidColorBrush(Color.White) };
targets[i] = new PointF(points[i].X, points[i].Y);
}
// We set colors manually since white points are kinda boring (notice, that we use alpha values)
pointGroups[0].Brush = new SolidColorBrush(Color.FromArgb(127, 255, 0, 0));
pointGroups[1].Brush = new SolidColorBrush(Color.FromArgb(127, 0, 255, 0));
pointGroups[2].Brush = new SolidColorBrush(Color.FromArgb(127, 0, 0, 255));
pointGroups[3].Brush = new SolidColorBrush(Color.FromArgb(127, 255, 0, 255));
pointGroups[4].Brush = new SolidColorBrush(Color.FromArgb(127, 255, 255, 0));
pointGroups[5].Brush = new SolidColorBrush(Color.FromArgb(127, 0, 255, 255));
while (true) while (true)
{ {
// Choose new target if we arrived // Calculate all the points
if (point.Contains(target)) for (int i = 0; i < NUM_POINTS; i++)
target = new PointF((float)(keyboard.KeyboardRectangle.X + (random.NextDouble() * keyboard.KeyboardRectangle.Width)), {
(float)(keyboard.KeyboardRectangle.Y + (random.NextDouble() * keyboard.KeyboardRectangle.Height))); // Choose new target if we arrived
else if (points[i].Contains(targets[i]))
// Calculate movement targets[i] = new PointF((float)(keyboard.KeyboardRectangle.X + (random.NextDouble() * keyboard.KeyboardRectangle.Width)),
point.Location = Interpolate(point.Location, target, SPEED); // It would be better to calculate from the center of our rectangle but the easy way is enough here (float)(keyboard.KeyboardRectangle.Y + (random.NextDouble() * keyboard.KeyboardRectangle.Height)));
else
// Calculate movement
points[i].Location = Interpolate(points[i].Location, targets[i], SPEED); // It would be better to calculate from the center of our rectangle but the easy way is enough here
// Move our rectangle to the new position // Move our rectangle to the new position
pointGroup.Rectangle = point; pointGroups[i].Rectangle = points[i];
}
// Update changed leds // Update changed leds
keyboard.UpdateLeds(); keyboard.UpdateLeds();

49
Helper/ColorHelper.cs Normal file
View File

@ -0,0 +1,49 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
namespace CUE.NET.Helper
{
public static class ColorHelper
{
public static float GetFloatA(this Color color)
{
return color.A / 255f;
}
public static float GetFloatR(this Color color)
{
return color.R / 255f;
}
public static float GetFloatG(this Color color)
{
return color.G / 255f;
}
public static float GetFloatB(this Color color)
{
return color.B / 255f;
}
public static Color Blend(this Color bg, Color fg)
{
if (fg.A == 255)
return fg;
if (fg.A == 0)
return bg;
float resultA = (1 - (1 - fg.GetFloatA()) * (1 - bg.GetFloatA()));
float resultR = (fg.GetFloatR() * fg.GetFloatA() / resultA + bg.GetFloatR() * bg.GetFloatA() * (1 - fg.GetFloatA()) / resultA);
float resultG = (fg.GetFloatG() * fg.GetFloatA() / resultA + bg.GetFloatG() * bg.GetFloatA() * (1 - fg.GetFloatA()) / resultA);
float resultB = (fg.GetFloatB() * fg.GetFloatA() / resultA + bg.GetFloatB() * bg.GetFloatA() * (1 - fg.GetFloatA()) / resultA);
return CreateColorFromFloat(resultA, resultR, resultG, resultB);
}
public static Color CreateColorFromFloat(float a, float r, float g, float b)
{
return Color.FromArgb((int)(a * 255), (int)(r * 255), (int)(g * 255), (int)(b * 255));
}
}
}