Skip to content

Commit 94e28e7

Browse files
authored
Fixing System.Drawing color struct (#221)
1 parent 907b833 commit 94e28e7

File tree

7 files changed

+194
-14
lines changed

7 files changed

+194
-14
lines changed

devices/System.Drawing/System.Drawing.cs

+8-12
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ internal Color(uint color)
1818
/// <summary>
1919
/// Gets the alpha component value of this System.Drawing.Color structure.
2020
/// </summary>
21-
public byte A { get => (byte)((_color >> 24) & 0xFF); }
21+
public byte A { get => (byte)(_color >> 24); }
2222

2323
/// <summary>
2424
/// Gets the blue component value of this System.Drawing.Color structure.
2525
/// </summary>
26-
public byte B { get => (byte)((_color >> 16) & 0xFF); }
26+
public byte B { get => (byte)(_color); }
2727

2828
/// <summary>
2929
/// Gets the red component value of this System.Drawing.Color structure.
3030
/// </summary>
31-
public byte R { get => (byte)(_color & 0xFF); }
31+
public byte R { get => (byte)(_color >> 16); }
3232

3333
/// <summary>
3434
/// Gets the green component value of this System.Drawing.Color structure.
3535
/// </summary>
36-
public byte G { get => (byte)((_color >> 8) & 0xFF); }
36+
public byte G { get => (byte)(_color >> 8); }
3737

3838
/// <summary>
3939
/// Creates a System.Drawing.Color structure from the specified 8-bit color values
@@ -47,7 +47,7 @@ internal Color(uint color)
4747
/// <returns>The System.Drawing.Color structure that this method creates.</returns>
4848
public static Color FromArgb(int r, int g, int b)
4949
{
50-
return new Color((uint)((0xFF << 24) | ((b & 0xFF) << 16) | ((g & 0xFF) << 8) | (r & 0xFF)));
50+
return new Color((uint)((0xFF << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)));
5151
}
5252

5353
/// <summary>
@@ -63,7 +63,7 @@ public static Color FromArgb(int r, int g, int b)
6363
/// <returns>The System.Drawing.Color structure that this method creates.</returns>
6464
public static Color FromArgb(int a, int r, int g, int b)
6565
{
66-
return new Color((uint)(((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((g & 0xFF) << 8) | (r & 0xFF)));
66+
return new Color((uint)(((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)));
6767
}
6868

6969
/// <summary>
@@ -179,12 +179,8 @@ public float GetSaturation()
179179
return (max == 0) ? 0.0f : (1.0f - (min / max));
180180
}
181181

182-
//
183-
// Summary:
184-
// Gets the 32-bit ARGB value of this System.Drawing.Color structure.
185-
//
186-
// Returns:
187-
// The 32-bit ARGB value of this System.Drawing.Color.
182+
/// <Summary>Gets the 32-bit ARGB value of this System.Drawing.Color structure.</Summary>
183+
/// <returns>The 32-bit ARGB value of this System.Drawing.Color.</returns>
188184
public int ToArgb() => (int)_color;
189185

190186
#region Known colors

devices/System.Drawing/System.Drawing.sln

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31613.86
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32014.148
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "System.Drawing", "System.Drawing.nfproj", "{2C586013-F59A-4D6F-BB9D-A97DB6B91DF8}"
77
EndProject
8+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "System.Drawing.Tests", "..\..\tests\System.Drawing.Tests\System.Drawing.Tests.nfproj", "{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -17,6 +19,12 @@ Global
1719
{2C586013-F59A-4D6F-BB9D-A97DB6B91DF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
1820
{2C586013-F59A-4D6F-BB9D-A97DB6B91DF8}.Release|Any CPU.Build.0 = Release|Any CPU
1921
{2C586013-F59A-4D6F-BB9D-A97DB6B91DF8}.Release|Any CPU.Deploy.0 = Release|Any CPU
22+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
25+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{3B8E1B85-9BB6-44B6-A36D-1DA12873BDE9}.Release|Any CPU.Deploy.0 = Release|Any CPU
2028
EndGlobalSection
2129
GlobalSection(SolutionProperties) = preSolution
2230
HideSolutionNode = FALSE
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using nanoFramework.TestFramework;
2+
using System;
3+
4+
namespace System.Drawing.Tests
5+
{
6+
[TestClass]
7+
public class ColorTests
8+
{
9+
[TestMethod]
10+
public void ColorFromARGBTest()
11+
{
12+
// Arrange
13+
Color color = Color.FromArgb(0x12345678);
14+
15+
// Assert
16+
Assert.Equal((byte)0x12, color.A, "A should be 0x12");
17+
Assert.Equal((byte)0x34, color.R, "R should be 0x34");
18+
Assert.Equal((byte)0x56, color.G, "G should be 0x56");
19+
Assert.Equal((byte)0x78, color.B, "B should be 0x78");
20+
}
21+
22+
[TestMethod]
23+
public void ColorFromARGBIndividualTest()
24+
{
25+
// Arrange
26+
Color color = Color.FromArgb(0x12, 0x34, 0x56, 0x78);
27+
28+
// Assert
29+
Assert.Equal((byte)0x12, color.A, "A should be 0x12");
30+
Assert.Equal((byte)0x34, color.R, "R should be 0x34");
31+
Assert.Equal((byte)0x56, color.G, "G should be 0x56");
32+
Assert.Equal((byte)0x78, color.B, "B should be 0x78");
33+
}
34+
35+
[TestMethod]
36+
public void ColorFromARGBWithoutATest()
37+
{
38+
// Arrange
39+
Color color = Color.FromArgb(0x34, 0x56, 0x78);
40+
41+
// Assert
42+
Assert.Equal((byte)0xFF, color.A, "A should be 0xFF");
43+
Assert.Equal((byte)0x34, color.R, "R should be 0x34");
44+
Assert.Equal((byte)0x56, color.G, "G should be 0x56");
45+
Assert.Equal((byte)0x78, color.B, "B should be 0x78");
46+
}
47+
48+
[TestMethod]
49+
public void ColorFromKnownColorTest()
50+
{
51+
// Arrange
52+
Color color = Color.Red;
53+
54+
// Assert
55+
Assert.Equal((byte)0xFF, color.A, "A should be 0xFF");
56+
Assert.Equal((byte)0xFF, color.R, "R should be 0xFF");
57+
Assert.Equal((byte)0x00, color.G, "G should be 0x00");
58+
Assert.Equal((byte)0x00, color.B, "B should be 0x00");
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")]
12+
[assembly: AssemblyTrademark("")]
13+
[assembly: AssemblyCulture("")]
14+
15+
// Setting ComVisible to false makes the types in this assembly not visible
16+
// to COM components. If you need to access a type in this assembly from
17+
// COM, set the ComVisible attribute to true on that type.
18+
[assembly: ComVisible(false)]
19+
20+
// Version information for an assembly consists of the following four values:
21+
//
22+
// Major Version
23+
// Minor Version
24+
// Build Number
25+
// Revision
26+
//
27+
// You can specify all the values or you can default the Build and Revision Numbers
28+
// by using the '*' as shown below:
29+
// [assembly: AssemblyVersion("1.0.*")]
30+
[assembly: AssemblyVersion("1.0.0.0")]
31+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<ItemGroup>
8+
<ProjectCapability Include="TestContainer" />
9+
</ItemGroup>
10+
<PropertyGroup>
11+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
13+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<ProjectGuid>3b8e1b85-9bb6-44b6-a36d-1da12873bde9</ProjectGuid>
15+
<OutputType>Library</OutputType>
16+
<AppDesignerFolder>Properties</AppDesignerFolder>
17+
<FileAlignment>512</FileAlignment>
18+
<RootNamespace>System.Drawing.Tests</RootNamespace>
19+
<AssemblyName>NFUnitTest</AssemblyName>
20+
<IsCodedUITest>False</IsCodedUITest>
21+
<IsTestProject>true</IsTestProject>
22+
<TestProjectType>UnitTest</TestProjectType>
23+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24+
</PropertyGroup>
25+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
26+
<PropertyGroup>
27+
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
28+
</PropertyGroup>
29+
<ItemGroup>
30+
<Compile Include="ColorTests.cs" />
31+
<Compile Include="Properties\AssemblyInfo.cs" />
32+
</ItemGroup>
33+
<ItemGroup>
34+
<Reference Include="mscorlib">
35+
<HintPath>..\..\devices\System.Drawing\packages\nanoFramework.CoreLibrary.1.11.7\lib\mscorlib.dll</HintPath>
36+
</Reference>
37+
<Reference Include="nanoFramework.TestFramework">
38+
<HintPath>..\..\devices\System.Drawing\packages\nanoFramework.TestFramework.1.0.166\lib\nanoFramework.TestFramework.dll</HintPath>
39+
</Reference>
40+
<Reference Include="nanoFramework.UnitTestLauncher">
41+
<HintPath>..\..\devices\System.Drawing\packages\nanoFramework.TestFramework.1.0.166\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
42+
</Reference>
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Include="nano.runsettings" />
46+
<None Include="packages.config" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ProjectReference Include="..\..\devices\System.Drawing\System.Drawing.nfproj" />
50+
</ItemGroup>
51+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
52+
<!-- MANUAL UPDATE HERE -->
53+
<Import Project="..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets" Condition="Exists('..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets')" />
54+
<ProjectExtensions>
55+
<ProjectCapabilities>
56+
<ProjectConfigurationsDeclaredAsItems />
57+
</ProjectCapabilities>
58+
</ProjectExtensions>
59+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
60+
<PropertyGroup>
61+
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
62+
</PropertyGroup>
63+
<Warning Condition="!Exists('..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
64+
</Target>
65+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<!-- Configurations that affect the Test Framework -->
4+
<RunConfiguration>
5+
<MaxCpuCount>1</MaxCpuCount>
6+
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
7+
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
8+
<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
9+
</RunConfiguration>
10+
<nanoFrameworkAdapter>
11+
<Logging>None</Logging>
12+
<IsRealHardware>False</IsRealHardware>
13+
</nanoFrameworkAdapter>
14+
</RunSettings>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.11.7" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.TestFramework" version="1.0.166" targetFramework="netnanoframework10" developmentDependency="true" />
5+
</packages>

0 commit comments

Comments
 (0)