1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Improved color-calculation of novation devices

This commit is contained in:
Darth Affe 2017-08-16 20:36:43 +02:00
parent 5440f9844f
commit 3a9d8e2e6d
9 changed files with 134 additions and 40 deletions

View File

@ -0,0 +1,33 @@
using System;
namespace RGB.NET.Devices.Novation.Attributes
{
/// <summary>
/// Specifies the color-capability of a field.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class ColorCapabilityAttribute : Attribute
{
#region Properties & Fields
/// <summary>
/// Gets the Id.
/// </summary>
public NovationColorCapabilities Capability { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ColorCapabilityAttribute"/> class.
/// </summary>
/// <param name="capability">The capability.</param>
public ColorCapabilityAttribute(NovationColorCapabilities capability)
{
this.Capability = capability;
}
#endregion
}
}

View File

@ -0,0 +1,16 @@
#pragma warning disable 1591
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Represents the color-capabilities of a novation device.
/// </summary>
public enum NovationColorCapabilities
{
None,
RGB,
LimitedRG
}
}

View File

@ -12,6 +12,7 @@ namespace RGB.NET.Devices.Novation
public enum NovationDevices
{
[DeviceId("Launchpad S")]
[ColorCapability(NovationColorCapabilities.LimitedRG)]
LaunchpadS
}
}

View File

@ -16,11 +16,12 @@ namespace RGB.NET.Devices.Novation
#region Properties & Fields
private readonly OutputDevice _outputDevice;
private readonly NovationRGBDeviceInfo _deviceInfo;
/// <summary>
/// Gets information about the <see cref="NovationRGBDevice"/>.
/// </summary>
public override IRGBDeviceInfo DeviceInfo { get; }
public override IRGBDeviceInfo DeviceInfo => _deviceInfo;
#endregion
@ -32,7 +33,7 @@ namespace RGB.NET.Devices.Novation
/// <param name="info">The generic information provided by Novation for the device.</param>
protected NovationRGBDevice(NovationRGBDeviceInfo info)
{
this.DeviceInfo = info;
_deviceInfo = info;
_outputDevice = new OutputDevice(info.DeviceId);
}
@ -112,46 +113,72 @@ namespace RGB.NET.Devices.Novation
{
foreach (Led led in leds)
{
NovationLedId ledId = (NovationLedId)led.Id;
int color = 0;
if (led.Color.R > 0)
{
color = 1;
if (led.Color.R > 127)
color = 2;
if (led.Color.R == 255)
color = 3;
}
if (led.Color.G > 0)
{
color = 16;
if (led.Color.G > 127)
color = 32;
if (led.Color.G == 255)
color = 48;
}
if ((led.Color.R > 0) && (led.Color.G > 0))
{
color = 17;
if (((led.Color.R > 127) && (led.Color.G < 127)) || ((led.Color.R < 127) && (led.Color.G > 127)))
color = 34;
if ((led.Color.R > 127) && (led.Color.G > 127))
color = 51;
}
NovationLedId ledId = led.Id as NovationLedId;
if (ledId == null) continue;
int color = ConvertColor(led.Color);
SendMessage(ledId.LedId.GetStatus(), ledId.LedId.GetId(), color);
}
}
}
private void SendMessage(int status, int data1, int data2)
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColor(Color color)
{
switch (_deviceInfo.ColorCapabilities)
{
case NovationColorCapabilities.RGB:
return ConvertColorFull(color);
case NovationColorCapabilities.LimitedRG:
return ConvertColorLimited(color);
default:
return 0;
}
}
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice"/>.
/// The conversion uses the full rgb-range.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColorFull(Color color)
{
//TODO DarthAffe 16.08.2017: How are colors for full rgb devices encoded?
return 0;
}
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice"/>.
/// The conversion uses only a limited amount of colors (3 red, 3 yellow, 3 green).
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColorLimited(Color color)
{
if ((color.Hue >= 330) || (color.Hue < 30))
return (int)Math.Ceiling(color.Value * 3); // red with brightness 1, 2 or 3
if ((color.Hue >= 30) && (color.Hue < 90)) // yellow with brightness 17, 34 or 51
return (int)Math.Ceiling(color.Value * 17);
if ((color.Hue >= 90) && (color.Hue < 150)) // green with brightness 16, 32 or 48
return (int)Math.Ceiling(color.Value * 16);
return 0;
}
/// <summary>
/// Sends a message to the <see cref="NovationRGBDevice"/>.
/// </summary>
/// <param name="status">The status-code of the message.</param>
/// <param name="data1">The first data-package of the message.</param>
/// <param name="data2">The second data-package of the message.</param>
protected virtual void SendMessage(int status, int data1, int data2)
{
ShortMessage shortMessage = new ShortMessage(Convert.ToByte(status), Convert.ToByte(data1), Convert.ToByte(data2));
_outputDevice.SendShort(shortMessage.Message);

View File

@ -25,6 +25,11 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc />
public RGBDeviceLighting Lighting => RGBDeviceLighting.Key;
/// <summary>
/// Gets the <see cref="NovationColorCapabilities"/> of the <see cref="IRGBDevice"/>.
/// </summary>
public NovationColorCapabilities ColorCapabilities { get; }
/// <summary>
/// Gets the (midi)-id of the <see cref="IRGBDevice"/>..
/// </summary>
@ -40,11 +45,13 @@ namespace RGB.NET.Devices.Novation
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="model">The represented device model.</param>
/// <param name="deviceId">The (midi)-id of the <see cref="IRGBDevice"/>.</param>
internal NovationRGBDeviceInfo(RGBDeviceType deviceType, string model, int deviceId)
/// <param name="colorCapabilities">The <see cref="NovationColorCapabilities"/> of the <see cref="IRGBDevice"/>.</param>
internal NovationRGBDeviceInfo(RGBDeviceType deviceType, string model, int deviceId, NovationColorCapabilities colorCapabilities)
{
this.DeviceType = deviceType;
this.Model = model;
this.DeviceId = deviceId;
this.ColorCapabilities = colorCapabilities;
}
#endregion

View File

@ -16,6 +16,13 @@ namespace RGB.NET.Devices.Novation
/// <returns>The value of the <see cref="DeviceIdAttribute"/> of the source.</returns>
internal static string GetDeviceId(this Enum source) => source.GetAttribute<DeviceIdAttribute>()?.Id;
/// <summary>
/// Gets the value of the <see cref="ColorCapabilityAttribute"/>.
/// </summary>
/// <param name="source">The enum value to get the description from.</param>
/// <returns>The value of the <see cref="ColorCapabilityAttribute"/> of the source.</returns>
internal static NovationColorCapabilities GetColorCapability(this Enum source) => source.GetAttribute<ColorCapabilityAttribute>()?.Capability ?? NovationColorCapabilities.None;
/// <summary>
/// Gets the attribute of type T.
/// </summary>

View File

@ -15,8 +15,9 @@ namespace RGB.NET.Devices.Novation
/// </summary>
/// <param name="model">The represented device model.</param>
/// <param name="deviceId"></param>
internal NovationLaunchpadRGBDeviceInfo(string model, int deviceId)
: base(RGBDeviceType.LedMatrix, model, deviceId)
/// <param name="colorCapabilities">The <see cref="NovationColorCapabilities"/> of the <see cref="IRGBDevice"/>.</param>
internal NovationLaunchpadRGBDeviceInfo(string model, int deviceId, NovationColorCapabilities colorCapabilities)
: base(RGBDeviceType.LedMatrix, model, deviceId, colorCapabilities)
{
Image = new Uri(PathHelper.GetAbsolutePath($@"Images\Novation\Launchpads\{Model.Replace(" ", string.Empty).ToUpper()}.png"), UriKind.Absolute);
}

View File

@ -75,7 +75,7 @@ namespace RGB.NET.Devices.Novation
if (deviceId == null) continue;
NovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index));
NovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, deviceId.GetColorCapability()));
device.Initialize();
devices.Add(device);
}

View File

@ -49,7 +49,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\ColorCapabilityAttribute.cs" />
<Compile Include="Attributes\DeviceIdAttribute.cs" />
<Compile Include="Enum\NovationColorCapabilities.cs" />
<Compile Include="Enum\NovationDevices.cs" />
<Compile Include="Enum\NovationLedIds.cs" />
<Compile Include="Generic\NovationLedId.cs" />