Skip to content

Commit

Permalink
Merge pull request #2 from michaldivis/vNext
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
michaldivis committed Sep 15, 2022
2 parents 672b103 + 86e79b5 commit f10daba
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

C# color helpers - color blender.

> What's the resulting color of #AABBCC with 70% opacity on a white background?
**That's what this library is for!**

## Nuget

[![Nuget](https://img.shields.io/nuget/v/Divis.DarkColors?label=Divis.DarkColors)](https://www.nuget.org/packages/Divis.DarkColors/)
Expand All @@ -22,19 +26,24 @@ Blend multiple colors together with transparency support.

### Basic
```csharp
var baseColor = Color.FromArgb(0, 0, 0); //The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
//The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0);

var colorToAdd = Color.FromArgb(125, 55, 13); //A color to add. This one can have transparency.
//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(125, 55, 13);

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
```

### Advanced
```csharp
var baseColor = Color.FromArgb(0, 0, 0); //The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
//The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0);

var colorToAdd = Color.FromArgb(127, 125, 55, 13); //A color to add. This one can have transparency.
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //The color's amount is set to 50% and it's alpha channel is at 50% so in the result, only 25% of this color will be added on top of the base color.
//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
//The color's amount is set to 50% and it's alpha channel is at 50% so in the result, only 25% of this color will be added on top of the base color.
var colorToAddLayer = new ColorLayer(colorToAdd, 50);

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAddLayer);

Expand All @@ -49,23 +58,27 @@ var threeColorsCombined = ColorBlender.Combine(baseColor, colorToAddLayer, anoth
Using the `Color`'s alpha channel:
```csharp
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(127, 125, 55, 13); //set 50% transparency using the color Alpha channel
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
```

Using the `ColorLayer`'s `AmountPercentage` property:
```csharp
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(125, 55, 13);
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //set 50% transparency using the color AmountPercentage property of the ColorLayer
//set 50% transparency using the color AmountPercentage property of the ColorLayer
var colorToAddLayer = new ColorLayer(colorToAdd, 50);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
```

Using both the `Color`'s alpha channel and `ColorLayer`'s `AmountPercentage` property:
```csharp
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(127, 125, 55, 13); //set 50% transparency using the color Alpha channel
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //set 50% transparency using the color AmountPercentage property of the ColorLayer. The resulting color will only be added by 25% because both color's Alpha and layer's AmountPercentage were used.
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
//set 50% transparency using the color AmountPercentage property of the ColorLayer. The resulting color will only be added by 25% because both color's Alpha and layer's AmountPercentage were used.
var colorToAddLayer = new ColorLayer(colorToAdd, 50);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
```

Expand Down
27 changes: 18 additions & 9 deletions sample/SampleApp/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ private static Color Hex(string hex)

private void SimpleExample()
{
var baseColor = Color.FromArgb(0, 0, 0); //The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
//The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0);

var colorToAdd = Color.FromArgb(125, 55, 13); //A color to add. This one can have transparency.
//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(125, 55, 13);

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
}

private void AdvancedExample()
{
var baseColor = Color.FromArgb(0, 0, 0); //The base color. This one can't be transparent. If it is, the alpha channel will be ignored.
// The base color.This one can't be transparent. If it is, the alpha channel will be ignored.
var baseColor = Color.FromArgb(0, 0, 0);

var colorToAdd = Color.FromArgb(127, 125, 55, 13); //A color to add. This one can have transparency.
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //The color's amount is set to 50% and it's alpha channel is at 50% so in the result, only 25% of this color will be added on top of the base color.
//A color to add. This one can have transparency.
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
//The color's amount is set to 50% and it's alpha channel is at 50% so in the result, only 25% of this color will be added on top of the base color.
var colorToAddLayer = new ColorLayer(colorToAdd, 50);

var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAddLayer);

Expand All @@ -63,23 +68,27 @@ private void AdvancedExample()
private void TransparencyUsingAlphaExample()
{
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(127, 125, 55, 13); //set 50% transparency using the color Alpha channel
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
}

private void TransparencyUsingAmountExample()
{
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(125, 55, 13);
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //set 50% transparency using the color AmountPercentage property of the ColorLayer
//set 50% transparency using the color AmountPercentage property of the ColorLayer
var colorToAddLayer = new ColorLayer(colorToAdd, 50);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
}

private void TransparencyUsingAlphaAndAmountExample()
{
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(127, 125, 55, 13); //set 50% transparency using the color Alpha channel
var colorToAddLayer = new ColorLayer(colorToAdd, 50); //set 50% transparency using the color AmountPercentage property of the ColorLayer. The resulting color will only be added by 25% because both color's Alpha and layer's AmountPercentage were used.
//set 50% transparency using the color Alpha channel
var colorToAdd = Color.FromArgb(127, 125, 55, 13);
//set 50% transparency using the color AmountPercentage property of the ColorLayer. The resulting color will only be added by 25% because both color's Alpha and layer's AmountPercentage were used.
var colorToAddLayer = new ColorLayer(colorToAdd, 50);
var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
}

Expand Down
11 changes: 8 additions & 3 deletions src/DarkColors/ColorBlender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Color Combine(Color nonTransparentBase, params Color[] colors)

foreach (var color in colors)
{
result = Blend(result, color, 100);
result = Blend(result, color);
}

return result;
Expand All @@ -33,7 +33,7 @@ public static Color Combine(Color nonTransparentBase, params Color[] colors)
/// <returns>Color blending result</returns>
public static Color Combine(Color nonTransparentBase, Color color)
{
return Blend(nonTransparentBase, color, 100);
return Blend(nonTransparentBase, color);
}

/// <summary>
Expand Down Expand Up @@ -67,8 +67,13 @@ public static Color Combine(Color nonTransparentBase, ColorLayer layer)
return Blend(nonTransparentBase, layer.Color, amount);
}

private static Color Blend(Color source, Color added, double addedManualAmount)
private static Color Blend(Color source, Color added, double addedManualAmount = 1d)
{
if(addedManualAmount < 0d || addedManualAmount > 1d)
{
throw new ArgumentOutOfRangeException(nameof(addedManualAmount), addedManualAmount, "Amount has to be in range 0-1");
}

var addedTransparencyAmount = (double)added.A / 255;
var addedAmount = addedManualAmount * addedTransparencyAmount;
var r = BlendChannel(source.R, added.R, addedAmount);
Expand Down
2 changes: 1 addition & 1 deletion src/DarkColors/DarkColors.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>Divis.DarkColors</PackageId>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<Authors>michaldivis</Authors>
<Company>Michal Diviš</Company>
<Product>Dark Colors</Product>
Expand Down
26 changes: 22 additions & 4 deletions tests/DarkColorsTests/ColorBlenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ColorBlenderTests
[InlineData(25, "#10153c")]
[InlineData(65, "#29389e")]
[InlineData(100, "#4056F4")]
public void Combine_ShouldWork_WhenAddingSingeNonTransparentColor(int percentage, string expectedHex)
public void CombineWithLayers_ShouldWork_WhenAddingSingeNonTransparentColor(int percentage, string expectedHex)
{
var result = ColorBlender.Combine(Hex("#000"), new ColorLayer[]
{
Expand All @@ -23,7 +23,7 @@ public void Combine_ShouldWork_WhenAddingSingeNonTransparentColor(int percentage
[InlineData(25, "#080b1f")]
[InlineData(65, "#151c4f")]
[InlineData(100, "#202b7a")]
public void Combine_ShouldWork_WhenAddingSingeTransparentColor(int percentage, string expectedHex)
public void CombineWithLayers_ShouldWork_WhenAddingSingeTransparentColor(int percentage, string expectedHex)
{
var result = ColorBlender.Combine(Hex("#000"), new ColorLayer[]
{
Expand All @@ -33,7 +33,7 @@ public void Combine_ShouldWork_WhenAddingSingeTransparentColor(int percentage, s
}

[Fact]
public void Combine_ShouldWork_WhenAddingMultipleNonTransparentColors()
public void CombineWithLayers_ShouldWork_WhenAddingMultipleNonTransparentColors()
{
var result = ColorBlender.Combine(Hex("#000"), new ColorLayer[]
{
Expand All @@ -44,7 +44,7 @@ public void Combine_ShouldWork_WhenAddingMultipleNonTransparentColors()
}

[Fact]
public void Combine_ShouldWork_WhenAddingMultipleTransparentColors()
public void CombineWithLayers_ShouldWork_WhenAddingMultipleTransparentColors()
{
var result = ColorBlender.Combine(Hex("#000"), new ColorLayer[]
{
Expand All @@ -54,6 +54,24 @@ public void Combine_ShouldWork_WhenAddingMultipleTransparentColors()
AssertColorsMatch(result, Hex("#2b2121"));
}

[Theory]
[InlineData("#004056F4", "#000")] //0%
[InlineData("#404056F4", "#10153c")] //25%
[InlineData("#A64056F4", "#29389e")] //65%
[InlineData("#FF4056F4", "#4056F4")] //100%
public void CombineWithColors_ShouldWork_WhenAddingSingeColor(string inputHex, string expectedHex)
{
var result = ColorBlender.Combine(Hex("#000"), Hex(inputHex));
AssertColorsMatch(result, Hex(expectedHex));
}

[Fact]
public void CombineWithColors_ShouldWork_WhenAddingMultipleColors()
{
var result = ColorBlender.Combine(Hex("#000"), Hex("#634056F4"), Hex("#40B1740F"));
AssertColorsMatch(result, Hex("#3f364b"));
}

private static void AssertColorsMatch(Color actual, Color expected)
{
AssertValuesMatchApproximately(actual.A, expected.A, 1);
Expand Down

0 comments on commit f10daba

Please sign in to comment.