Skip to content

Commit

Permalink
Color transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeah69 committed Nov 8, 2020
1 parent 2364eca commit a8b3976
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
39 changes: 39 additions & 0 deletions MrMeeseeks.Test/Extensions/ColorExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Drawing;
using MrMeeseeks.Extensions;
using Xunit;

namespace MrMeeseeks.Test.Extensions
{
public class ColorExtensionsTests
{
public static IEnumerable<object[]> ToLongData =>
new [] {
new [] {
(object) Color.Red, 16711680
},
new [] {
(object) Color.Blue, 255
},
new [] {
(object) Color.Green, 32768
},
new [] {
(object) Color.Purple, 8388736
},
};

[Theory]
[MemberData(nameof(ToLongData))]
public void ToLong_GivenColor_ExpectedLong(Color color, long expected)
{
// Arrange

// Act
var result = color.ToLong();

// Assert
Assert.Equal(expected, result);
}
}
}
19 changes: 19 additions & 0 deletions MrMeeseeks/Extensions/ColorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;

namespace MrMeeseeks.Extensions
{
public static class ColorExtensions
{
public static long ToLong(this Color color)
{
long ret = color.A << 24 + color.R << 16 + color.G << 8 + color.B << 0;
ret <<= 8;
ret += color.R;
ret <<= 8;
ret += color.G;
ret <<= 8;
ret += color.B;
return ret;
}
}
}
14 changes: 14 additions & 0 deletions MrMeeseeks/Extensions/LongExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Drawing;

namespace MrMeeseeks.Extensions
{
public static class LongExtensions
{
public static Color ToColor(this long number) =>
Color.FromArgb(
(byte) (number >> 24 & 0xff),
(byte) (number >> 16 & 0xff),
(byte) (number >> 8 & 0xff),
(byte) (number & 0xff));
}
}

0 comments on commit a8b3976

Please sign in to comment.