diff --git a/CHANGELOG.md b/CHANGELOG.md index 94afdbe..ccf113f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Features - Update Colors using the `System.Windows.Media.Colors` as reference. - Update `MaterialUtils` and `GraphicsStyleUtils` with default 9 colors. +- Add `ColorExtension` with `ColorEquals`, `Lerp`, `ToColor` and `ToColorWithTransparency`. +- Add `ColorExtension` with `ToHex` for `Color` and `ColorWithTransparency` ### Tests - Test all color names in `System.Windows.Media.Colors` with `Colors`. - Test all 9 colors in `MaterialUtils` and `GraphicsStyleUtils`. +- Test for `ColorExternsion` ## [0.3.1] / 2024-01-09 - 2024-04-13 ### Features diff --git a/Directory.Build.props b/Directory.Build.props index 71cd1ec..2ee2d33 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 0.3.2-beta + 0.3.2-rc 2024 2025 true diff --git a/ricaun.Revit.DB.Shape.Tests/Color_Tests.cs b/ricaun.Revit.DB.Shape.Tests/Color_Tests.cs index 7123795..c1578b4 100644 --- a/ricaun.Revit.DB.Shape.Tests/Color_Tests.cs +++ b/ricaun.Revit.DB.Shape.Tests/Color_Tests.cs @@ -1,6 +1,7 @@ using Autodesk.Revit.DB; using NUnit.Framework; using ricaun.Revit.DB.Shape.Tests.Utils; +using ricaun.Revit.DB.Shape.Extensions; using System.Collections.Generic; namespace ricaun.Revit.DB.Shape.Tests @@ -45,7 +46,7 @@ public void Colors_Tests() var expectedColor = new Color(color.Value.R, color.Value.G, color.Value.B); if (colorsType.GetProperty(name)?.GetValue(null) is Color colorTest) { - Assert.IsTrue(colorTest.ColorEquals(expectedColor), + Assert.IsTrue(colorTest.ColorEquals(expectedColor), $"{name} Color ({colorTest.Red},{colorTest.Green},{colorTest.Blue}) is not equal to ({expectedColor.Red},{expectedColor.Green},{expectedColor.Blue})"); } else @@ -93,7 +94,64 @@ public void Colors_Index_Get_All_Tests() Assert.IsNotNull(Colors.Index.Get((byte)i)); } } - } + [Test] + public void ColorExtension_ColorEquals_Tests() + { + for (int i = 0; i < 256; i++) + { + var color = Colors.Index.Get((byte)i); + Assert.IsTrue(color.ColorEquals(color)); + + var colorT = color.ToColorWithTransparency(); + Assert.IsTrue(colorT.ColorEquals(colorT)); + Assert.IsTrue(colorT.ColorEquals(color)); + Assert.IsTrue(color.ColorEquals(colorT)); + + var colorT100 = color.ToColorWithTransparency(100); + Assert.IsFalse(colorT.ColorEquals(colorT100)); + Assert.IsFalse(colorT100.ColorEquals(colorT)); + + var _color = colorT.ToColor(); + Assert.IsTrue(color.ColorEquals(_color)); + } + } + + [Test] + public void ColorExtension_Lerp_Tests() + { + var color = Colors.Black.Lerp(Colors.Gray, 0.5); + var colorExpected = new Color(64, 64, 64); + Assert.IsTrue(color.ColorEquals(colorExpected)); + } + + [Test] + public void ColorExtension_ToHex_Tests() + { + var tests = new Dictionary() + { + { "#000000", Colors.Black }, + { "#FF0000", Colors.Red }, + { "#008000", Colors.Green }, + { "#00FF00", Colors.Lime }, + { "#0000FF", Colors.Blue }, + { "#FFFF00", Colors.Yellow }, + { "#00FFFF", Colors.Cyan }, + { "#FF00FF", Colors.Magenta }, + { "#808080", Colors.Gray }, + { "#FFFFFF", Colors.White }, + }; + + foreach (var test in tests) + { + Assert.AreEqual(test.Key, test.Value.ToHex()); + } + + foreach (var test in tests) + { + Assert.AreEqual(test.Key, test.Value.ToColorWithTransparency().ToHex()); + } + } + } } \ No newline at end of file diff --git a/ricaun.Revit.DB.Shape.Tests/LineColorCreate_Tests.cs b/ricaun.Revit.DB.Shape.Tests/LineColorCreate_Tests.cs index e830a8e..c8ae523 100644 --- a/ricaun.Revit.DB.Shape.Tests/LineColorCreate_Tests.cs +++ b/ricaun.Revit.DB.Shape.Tests/LineColorCreate_Tests.cs @@ -1,6 +1,7 @@ using Autodesk.Revit.DB; using NUnit.Framework; using ricaun.Revit.DB.Shape.Tests.Utils; +using ricaun.Revit.DB.Shape.Extensions; using System.Collections.Generic; namespace ricaun.Revit.DB.Shape.Tests diff --git a/ricaun.Revit.DB.Shape.Tests/MaterialCreate_Tests.cs b/ricaun.Revit.DB.Shape.Tests/MaterialCreate_Tests.cs index 171514e..a3ba058 100644 --- a/ricaun.Revit.DB.Shape.Tests/MaterialCreate_Tests.cs +++ b/ricaun.Revit.DB.Shape.Tests/MaterialCreate_Tests.cs @@ -2,6 +2,7 @@ using Autodesk.Revit.DB; using NUnit.Framework; using ricaun.Revit.DB.Shape.Tests.Utils; +using ricaun.Revit.DB.Shape.Extensions; using System; using System.Collections.Generic; diff --git a/ricaun.Revit.DB.Shape.Tests/Utils/ColorExtension.cs b/ricaun.Revit.DB.Shape.Tests/Utils/ColorExtension.cs index 1484f5f..9171db3 100644 --- a/ricaun.Revit.DB.Shape.Tests/Utils/ColorExtension.cs +++ b/ricaun.Revit.DB.Shape.Tests/Utils/ColorExtension.cs @@ -1,30 +1,30 @@ -using Autodesk.Revit.DB; +//using Autodesk.Revit.DB; -namespace ricaun.Revit.DB.Shape.Tests.Utils -{ - public static class ColorExtension - { - public static bool ColorEquals(this Color colorA, Color colorB) - { - return colorA.Red == colorB.Red && colorA.Green == colorB.Green && colorA.Blue == colorB.Blue; - } +//namespace ricaun.Revit.DB.Shape.Tests.Utils +//{ +// public static class ColorExtension +// { +// public static bool ColorEquals(this Color colorA, Color colorB) +// { +// return colorA.Red == colorB.Red && colorA.Green == colorB.Green && colorA.Blue == colorB.Blue; +// } - public static bool ColorEquals(this Color colorA, ColorWithTransparency colorB) - { - return colorA.Red == colorB.GetRed() && colorA.Green == colorB.GetGreen() && colorA.Blue == colorB.GetBlue(); - } +// public static bool ColorEquals(this Color colorA, ColorWithTransparency colorB) +// { +// return colorA.Red == colorB.GetRed() && colorA.Green == colorB.GetGreen() && colorA.Blue == colorB.GetBlue(); +// } - public static bool ColorEquals(this ColorWithTransparency colorA, Color colorB) - { - return colorB.ColorEquals(colorA); - } +// public static bool ColorEquals(this ColorWithTransparency colorA, Color colorB) +// { +// return colorB.ColorEquals(colorA); +// } - public static bool ColorEquals(this ColorWithTransparency colorA, ColorWithTransparency colorB) - { - return colorA.GetRed() == colorB.GetRed() && - colorA.GetGreen() == colorB.GetGreen() && - colorA.GetBlue() == colorB.GetBlue() && - colorA.GetTransparency() == colorB.GetTransparency(); - } - } -} \ No newline at end of file +// public static bool ColorEquals(this ColorWithTransparency colorA, ColorWithTransparency colorB) +// { +// return colorA.GetRed() == colorB.GetRed() && +// colorA.GetGreen() == colorB.GetGreen() && +// colorA.GetBlue() == colorB.GetBlue() && +// colorA.GetTransparency() == colorB.GetTransparency(); +// } +// } +//} \ No newline at end of file diff --git a/ricaun.Revit.DB.Shape/Extensions/ColorExtension.cs b/ricaun.Revit.DB.Shape/Extensions/ColorExtension.cs new file mode 100644 index 0000000..669b002 --- /dev/null +++ b/ricaun.Revit.DB.Shape/Extensions/ColorExtension.cs @@ -0,0 +1,130 @@ +using Autodesk.Revit.DB; + +namespace ricaun.Revit.DB.Shape.Extensions +{ + /// + /// ColorExtension + /// + public static class ColorExtension + { + /// + /// Linearly interpolates between colors a and b by t. + /// + /// + /// + /// + public static Color Lerp(this Color colorA, Color colorB, double t) + { + byte r = (byte)(colorA.Red + (colorB.Red - colorA.Red) * t); + byte g = (byte)(colorA.Green + (colorB.Green - colorA.Green) * t); + byte b = (byte)(colorA.Blue + (colorB.Blue - colorA.Blue) * t); + return new Color(r, g, b); + } + + /// + /// ToColorWithTransparency + /// + /// + /// + /// + public static ColorWithTransparency ToColorWithTransparency(this Color color, byte alpha = byte.MaxValue) + { + if (color is null) return null; + var colorWithTransparency = new ColorWithTransparency(color.Red, color.Green, color.Blue, alpha); + return colorWithTransparency; + } + + /// + /// ToColor + /// + /// + /// + public static Color ToColor(this ColorWithTransparency colorWithTransparency) + { + if (colorWithTransparency is null) return null; + var color = new Color( + (byte)colorWithTransparency.GetRed(), + (byte)colorWithTransparency.GetGreen(), + (byte)colorWithTransparency.GetBlue()); + return color; + } + + #region Equals + /// + /// ColorEquals RGB + /// + /// + /// + public static bool ColorEquals(this Color colorA, Color colorB) + { + return colorA.Red == colorB.Red && colorA.Green == colorB.Green && colorA.Blue == colorB.Blue; + } + /// + /// ColorEquals RGB only the transparency is not compared. + /// + /// + /// + public static bool ColorEquals(this Color colorA, ColorWithTransparency colorB) + { + return colorA.Red == colorB.GetRed() && colorA.Green == colorB.GetGreen() && colorA.Blue == colorB.GetBlue(); + } + /// + /// ColorEquals RGB only the transparency is not compared. + /// + /// + /// + public static bool ColorEquals(this ColorWithTransparency colorA, Color colorB) + { + return colorB.ColorEquals(colorA); + } + /// + /// ColorEquals RGB and Transparency + /// + /// + /// + public static bool ColorEquals(this ColorWithTransparency colorA, ColorWithTransparency colorB) + { + return colorA.GetRed() == colorB.GetRed() && + colorA.GetGreen() == colorB.GetGreen() && + colorA.GetBlue() == colorB.GetBlue() && + colorA.GetTransparency() == colorB.GetTransparency(); + } + #endregion + + /// + /// Convert to #RRGGBB in hex format + /// + /// + /// + public static string ToHex(this Color color) + { + return ToHex(color.Red, color.Green, color.Blue); + } + + /// + /// Convert to #RRGGBB or #AARRGGBB in hex format + /// + /// + /// + public static string ToHex(this ColorWithTransparency color) + { + return ToHex((byte)color.GetRed(), (byte)color.GetGreen(), (byte)color.GetBlue(), (byte)color.GetTransparency()); + } + + /// + /// Convert to #RRGGBB or #AARRGGBB + /// + /// + /// + /// + /// + /// + public static string ToHex(byte red, byte green, byte blue, byte alpha = byte.MaxValue) + { + if (alpha == byte.MaxValue) + return string.Format("#{0:X2}{1:X2}{2:X2}", red, green, blue); + + return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", alpha, red, green, blue); + } + } +} diff --git a/ricaun.Revit.DB.Shape/XYZUtils.cs b/ricaun.Revit.DB.Shape/XYZUtils.cs index 75a3b99..4c3f12f 100644 --- a/ricaun.Revit.DB.Shape/XYZUtils.cs +++ b/ricaun.Revit.DB.Shape/XYZUtils.cs @@ -18,7 +18,7 @@ public static class XYZUtils /// /// /// - public static bool IsAlmostParallelTo(this XYZ value, XYZ source, double tolerance = Tolerance) + public static bool IsAlmostParallelTo(XYZ value, XYZ source, double tolerance = Tolerance) { return Math.Abs(value.DotProduct(source)) >= 1.0 - tolerance; }