diff --git a/RGB.NET.Devices.Novation/Attributes/ColorCapabilityAttribute.cs b/RGB.NET.Devices.Novation/Attributes/ColorCapabilityAttribute.cs new file mode 100644 index 0000000..d02aa60 --- /dev/null +++ b/RGB.NET.Devices.Novation/Attributes/ColorCapabilityAttribute.cs @@ -0,0 +1,33 @@ +using System; + +namespace RGB.NET.Devices.Novation.Attributes +{ + /// + /// Specifies the color-capability of a field. + /// + [AttributeUsage(AttributeTargets.Field)] + public class ColorCapabilityAttribute : Attribute + { + #region Properties & Fields + + /// + /// Gets the Id. + /// + public NovationColorCapabilities Capability { get; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + /// The capability. + public ColorCapabilityAttribute(NovationColorCapabilities capability) + { + this.Capability = capability; + } + + #endregion + } +} diff --git a/RGB.NET.Devices.Novation/Enum/NovationColorCapabilities.cs b/RGB.NET.Devices.Novation/Enum/NovationColorCapabilities.cs new file mode 100644 index 0000000..9f7091a --- /dev/null +++ b/RGB.NET.Devices.Novation/Enum/NovationColorCapabilities.cs @@ -0,0 +1,16 @@ +#pragma warning disable 1591 +// ReSharper disable InconsistentNaming +// ReSharper disable UnusedMember.Global + +namespace RGB.NET.Devices.Novation +{ + /// + /// Represents the color-capabilities of a novation device. + /// + public enum NovationColorCapabilities + { + None, + RGB, + LimitedRG + } +} diff --git a/RGB.NET.Devices.Novation/Enum/NovationDevices.cs b/RGB.NET.Devices.Novation/Enum/NovationDevices.cs index 6a4f2b6..ca16a62 100644 --- a/RGB.NET.Devices.Novation/Enum/NovationDevices.cs +++ b/RGB.NET.Devices.Novation/Enum/NovationDevices.cs @@ -12,6 +12,7 @@ namespace RGB.NET.Devices.Novation public enum NovationDevices { [DeviceId("Launchpad S")] + [ColorCapability(NovationColorCapabilities.LimitedRG)] LaunchpadS } } diff --git a/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs b/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs index cfde254..db4da64 100644 --- a/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs +++ b/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs @@ -16,11 +16,12 @@ namespace RGB.NET.Devices.Novation #region Properties & Fields private readonly OutputDevice _outputDevice; + private readonly NovationRGBDeviceInfo _deviceInfo; /// /// Gets information about the . /// - public override IRGBDeviceInfo DeviceInfo { get; } + public override IRGBDeviceInfo DeviceInfo => _deviceInfo; #endregion @@ -32,7 +33,7 @@ namespace RGB.NET.Devices.Novation /// The generic information provided by Novation for the device. 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) + /// + /// Convert a to its novation-representation depending on the of the . + /// + /// The to convert. + /// The novation-representation of the . + protected virtual int ConvertColor(Color color) + { + switch (_deviceInfo.ColorCapabilities) + { + case NovationColorCapabilities.RGB: + return ConvertColorFull(color); + case NovationColorCapabilities.LimitedRG: + return ConvertColorLimited(color); + default: + return 0; + } + } + + /// + /// Convert a to its novation-representation depending on the of the . + /// The conversion uses the full rgb-range. + /// + /// The to convert. + /// The novation-representation of the . + protected virtual int ConvertColorFull(Color color) + { + //TODO DarthAffe 16.08.2017: How are colors for full rgb devices encoded? + return 0; + } + + /// + /// Convert a to its novation-representation depending on the of the . + /// The conversion uses only a limited amount of colors (3 red, 3 yellow, 3 green). + /// + /// The to convert. + /// The novation-representation of the . + 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; + } + + /// + /// Sends a message to the . + /// + /// The status-code of the message. + /// The first data-package of the message. + /// The second data-package of the message. + 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); diff --git a/RGB.NET.Devices.Novation/Generic/NovationRGBDeviceInfo.cs b/RGB.NET.Devices.Novation/Generic/NovationRGBDeviceInfo.cs index eecbeb7..dbc703d 100644 --- a/RGB.NET.Devices.Novation/Generic/NovationRGBDeviceInfo.cs +++ b/RGB.NET.Devices.Novation/Generic/NovationRGBDeviceInfo.cs @@ -25,6 +25,11 @@ namespace RGB.NET.Devices.Novation /// public RGBDeviceLighting Lighting => RGBDeviceLighting.Key; + /// + /// Gets the of the . + /// + public NovationColorCapabilities ColorCapabilities { get; } + /// /// Gets the (midi)-id of the .. /// @@ -40,11 +45,13 @@ namespace RGB.NET.Devices.Novation /// The type of the . /// The represented device model. /// The (midi)-id of the . - internal NovationRGBDeviceInfo(RGBDeviceType deviceType, string model, int deviceId) + /// The of the . + internal NovationRGBDeviceInfo(RGBDeviceType deviceType, string model, int deviceId, NovationColorCapabilities colorCapabilities) { this.DeviceType = deviceType; this.Model = model; this.DeviceId = deviceId; + this.ColorCapabilities = colorCapabilities; } #endregion diff --git a/RGB.NET.Devices.Novation/Helper/EnumExtension.cs b/RGB.NET.Devices.Novation/Helper/EnumExtension.cs index d5f3e2c..9be8305 100644 --- a/RGB.NET.Devices.Novation/Helper/EnumExtension.cs +++ b/RGB.NET.Devices.Novation/Helper/EnumExtension.cs @@ -16,6 +16,13 @@ namespace RGB.NET.Devices.Novation /// The value of the of the source. internal static string GetDeviceId(this Enum source) => source.GetAttribute()?.Id; + /// + /// Gets the value of the . + /// + /// The enum value to get the description from. + /// The value of the of the source. + internal static NovationColorCapabilities GetColorCapability(this Enum source) => source.GetAttribute()?.Capability ?? NovationColorCapabilities.None; + /// /// Gets the attribute of type T. /// diff --git a/RGB.NET.Devices.Novation/Launchpad/NovationLaunchpadRGBDeviceInfo.cs b/RGB.NET.Devices.Novation/Launchpad/NovationLaunchpadRGBDeviceInfo.cs index cf134ec..6a47179 100644 --- a/RGB.NET.Devices.Novation/Launchpad/NovationLaunchpadRGBDeviceInfo.cs +++ b/RGB.NET.Devices.Novation/Launchpad/NovationLaunchpadRGBDeviceInfo.cs @@ -15,8 +15,9 @@ namespace RGB.NET.Devices.Novation /// /// The represented device model. /// - internal NovationLaunchpadRGBDeviceInfo(string model, int deviceId) - : base(RGBDeviceType.LedMatrix, model, deviceId) + /// The of the . + 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); } diff --git a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs index 7c50c45..cde43b5 100644 --- a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs +++ b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs @@ -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); } diff --git a/RGB.NET.Devices.Novation/RGB.NET.Devices.Novation.csproj b/RGB.NET.Devices.Novation/RGB.NET.Devices.Novation.csproj index f9f2316..3100502 100644 --- a/RGB.NET.Devices.Novation/RGB.NET.Devices.Novation.csproj +++ b/RGB.NET.Devices.Novation/RGB.NET.Devices.Novation.csproj @@ -49,7 +49,9 @@ + +