Skip to content

Commit 378fa25

Browse files
committed
Fix hue-based interpolation
1 parent 741e963 commit 378fa25

File tree

10 files changed

+256
-18
lines changed

10 files changed

+256
-18
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ These [can be overridden](#advanced-configuration-) using the `Configuration` pa
2020

2121
This library was initially written for personal projects since existing libraries had complex APIs or missing features.
2222
The goal of this library is to be intuitive and easy to use; performance is not a priority.
23+
It is also [extensively tested](Unicolour.Tests) against known colour values and other .NET libraries.
2324

2425
More functionality will be added over time.
2526

@@ -31,7 +32,7 @@ Targets .NET Standard 2.0 for use in .NET 5.0+, .NET Core 2.0+ and .NET Framewor
3132
dotnet add package Wacton.Unicolour
3233
```
3334

34-
2. Create a `Unicolour` from RGB or HSB/HSV values:
35+
2. Create a `Unicolour`:
3536
```c#
3637
using Wacton.Unicolour;
3738
...
@@ -64,6 +65,8 @@ var contrast = unicolour1.Contrast(unicolour2);
6465
var difference = unicolour1.DeltaE76(unicolour2);
6566
```
6667

68+
See also the [example code](Unicolour.Example/Program.cs), which uses `Unicolour` to generate gradients through different colour spaces.
69+
6770
## Advanced configuration 💡
6871
A `Configuration` parameter can be used to change the RGB model (e.g. Adobe RGB, wide-gamut RGB)
6972
and the white point of the XYZ colour space (e.g. D50 reference white used by ICC profiles).
95.6 KB
Binary file not shown.

Unicolour.Example/OFL.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2006 The Inconsolata Project Authors
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.

Unicolour.Example/Program.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using SixLabors.Fonts;
2+
using SixLabors.ImageSharp;
3+
using SixLabors.ImageSharp.Drawing.Processing;
4+
using SixLabors.ImageSharp.PixelFormats;
5+
using SixLabors.ImageSharp.Processing;
6+
using Wacton.Unicolour;
7+
8+
var startColour = Unicolour.FromHsb(260, 1.0, 0.33);
9+
var endColour = Unicolour.FromHsb(30, 0.66, 1.0);
10+
var backgroundRgba32 = AsRgba32(Unicolour.FromHex("#404046"));
11+
var textRgba32 = AsRgba32(Unicolour.FromHex("#E8E8FF"));
12+
13+
FontCollection collection = new();
14+
var fontFamily = collection.Add("Inconsolata-Regular.ttf");
15+
var font = fontFamily.CreateFont(32);
16+
17+
var gradientWidth = 1000;
18+
var gradientHeight = 200;
19+
20+
var image = new Image<Rgba32>(gradientWidth, gradientHeight * 3);
21+
image.Mutate(x => x.BackgroundColor(backgroundRgba32));
22+
23+
for (var x = 0; x < gradientWidth; x++)
24+
{
25+
var distance = x / (double)(gradientWidth - 1);
26+
var interpolatedRgb = startColour.InterpolateRgb(endColour, distance);
27+
var interpolatedHsb = startColour.InterpolateHsb(endColour, distance);
28+
var interpolatedHsl = startColour.InterpolateHsl(endColour, distance);
29+
SetPixels(x, interpolatedRgb, interpolatedHsb, interpolatedHsl);
30+
}
31+
32+
image.Save("gradients.png");
33+
34+
void SetPixels(int x, Unicolour viaRgb, Unicolour viaHsb, Unicolour viaHsl)
35+
{
36+
for (var y = 0; y < gradientHeight; y++)
37+
{
38+
image[x, y] = AsRgba32(viaRgb);
39+
image[x, y + 200] = AsRgba32(viaHsb);
40+
image[x, y + 400] = AsRgba32(viaHsl);
41+
}
42+
43+
PointF TextLocation(float targetY) => new(24, targetY + 24);
44+
image.Mutate(context => context.DrawText("RGB", font, textRgba32, TextLocation(0)));
45+
image.Mutate(context => context.DrawText("HSB", font, textRgba32, TextLocation(200)));
46+
image.Mutate(context => context.DrawText("HSL", font, textRgba32, TextLocation(400)));
47+
}
48+
49+
Rgba32 AsRgba32(Unicolour unicolour)
50+
{
51+
var rgb = unicolour.Rgb;
52+
return new Rgba32((byte) rgb.R255, (byte) rgb.G255, (byte) rgb.B255);
53+
}
54+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
<AssemblyName>Wacton.Unicolour.Example</AssemblyName>
10+
<RootNamespace>Wacton.Unicolour.Example</RootNamespace>
11+
<Authors>William Acton</Authors>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.0" />
16+
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\Unicolour\Unicolour.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<None Update="Inconsolata-Regular.ttf">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
27+
</ItemGroup>
28+
29+
</Project>

Unicolour.Tests/InterpolateHsbTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void SameColour()
2222
}
2323

2424
[Test]
25-
public void EquidistantForward()
25+
public void Equidistant()
2626
{
2727
var unicolour1 = Unicolour.FromHsb(0, 0, 0, 0);
2828
var unicolour2 = Unicolour.FromHsb(180, 1, 1);
@@ -34,7 +34,7 @@ public void EquidistantForward()
3434
}
3535

3636
[Test]
37-
public void EquidistantBackward()
37+
public void EquidistantViaRed()
3838
{
3939
var unicolour1 = Unicolour.FromHsb(0, 0, 0, 0);
4040
var unicolour2 = Unicolour.FromHsb(340, 0.5, 0.8, 0.2);
@@ -57,6 +57,18 @@ public void CloserToEndColour()
5757
AssertHsba(interpolated2, (45, 0.75, 0.25, 0.875));
5858
}
5959

60+
[Test]
61+
public void CloserToEndColourViaRed()
62+
{
63+
var unicolour1 = Unicolour.FromHsb(300, 1, 0);
64+
var unicolour2 = Unicolour.FromHsb(60, 0, 1, 0.5);
65+
var interpolated1 = unicolour1.InterpolateHsb(unicolour2, 0.75);
66+
var interpolated2 = unicolour2.InterpolateHsb(unicolour1, 0.75);
67+
68+
AssertHsba(interpolated1, (30, 0.25, 0.75, 0.625));
69+
AssertHsba(interpolated2, (330, 0.75, 0.25, 0.875));
70+
}
71+
6072
[Test]
6173
public void CloserToStartColour()
6274
{
@@ -69,6 +81,18 @@ public void CloserToStartColour()
6981
AssertHsba(interpolated2, (135, 0.25, 0.75, 0.625));
7082
}
7183

84+
[Test]
85+
public void CloserToStartColourViaRed()
86+
{
87+
var unicolour1 = Unicolour.FromHsb(300, 1, 0);
88+
var unicolour2 = Unicolour.FromHsb(60, 0, 1, 0.5);
89+
var interpolated1 = unicolour1.InterpolateHsb(unicolour2, 0.25);
90+
var interpolated2 = unicolour2.InterpolateHsb(unicolour1, 0.25);
91+
92+
AssertHsba(interpolated1, (330, 0.75, 0.25, 0.875));
93+
AssertHsba(interpolated2, (30, 0.25, 0.75, 0.625));
94+
}
95+
7296
[Test]
7397
public void BeyondEndColour()
7498
{

Unicolour.Tests/InterpolateHslTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void SameColour()
2222
}
2323

2424
[Test]
25-
public void EquidistantForward()
25+
public void Equidistant()
2626
{
2727
var unicolour1 = Unicolour.FromHsl(0, 0, 0, 0);
2828
var unicolour2 = Unicolour.FromHsl(180, 1, 1);
@@ -34,7 +34,7 @@ public void EquidistantForward()
3434
}
3535

3636
[Test]
37-
public void EquidistantBackward()
37+
public void EquidistantViaRed()
3838
{
3939
var unicolour1 = Unicolour.FromHsl(0, 0, 0, 0);
4040
var unicolour2 = Unicolour.FromHsl(340, 0.5, 0.8, 0.2);
@@ -57,6 +57,18 @@ public void CloserToEndColour()
5757
AssertHsla(interpolated2, (45, 0.75, 0.25, 0.875));
5858
}
5959

60+
[Test]
61+
public void CloserToEndColourViaRed()
62+
{
63+
var unicolour1 = Unicolour.FromHsl(300, 1, 0);
64+
var unicolour2 = Unicolour.FromHsl(60, 0, 1, 0.5);
65+
var interpolated1 = unicolour1.InterpolateHsl(unicolour2, 0.75);
66+
var interpolated2 = unicolour2.InterpolateHsl(unicolour1, 0.75);
67+
68+
AssertHsla(interpolated1, (30, 0.25, 0.75, 0.625));
69+
AssertHsla(interpolated2, (330, 0.75, 0.25, 0.875));
70+
}
71+
6072
[Test]
6173
public void CloserToStartColour()
6274
{
@@ -69,6 +81,18 @@ public void CloserToStartColour()
6981
AssertHsla(interpolated2, (135, 0.25, 0.75, 0.625));
7082
}
7183

84+
[Test]
85+
public void CloserToStartColourViaRed()
86+
{
87+
var unicolour1 = Unicolour.FromHsl(300, 1, 0);
88+
var unicolour2 = Unicolour.FromHsl(60, 0, 1, 0.5);
89+
var interpolated1 = unicolour1.InterpolateHsl(unicolour2, 0.25);
90+
var interpolated2 = unicolour2.InterpolateHsl(unicolour1, 0.25);
91+
92+
AssertHsla(interpolated1, (330, 0.75, 0.25, 0.875));
93+
AssertHsla(interpolated2, (30, 0.25, 0.75, 0.625));
94+
}
95+
7296
[Test]
7397
public void BeyondEndColour()
7498
{

Unicolour.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicolour", "Unicolour\Unic
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicolour.Tests", "Unicolour.Tests\Unicolour.Tests.csproj", "{43CC17D1-2A95-4554-9900-D97FA9CEA2D0}"
66
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicolour.Example", "Unicolour.Example\Unicolour.Example.csproj", "{DA851ED7-FCE5-4A17-818A-ED80BE673EBB}"
8+
EndProject
79
Global
810
GlobalSection(SolutionConfigurationPlatforms) = preSolution
911
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
1820
{43CC17D1-2A95-4554-9900-D97FA9CEA2D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1921
{43CC17D1-2A95-4554-9900-D97FA9CEA2D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
2022
{43CC17D1-2A95-4554-9900-D97FA9CEA2D0}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{DA851ED7-FCE5-4A17-818A-ED80BE673EBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{DA851ED7-FCE5-4A17-818A-ED80BE673EBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{DA851ED7-FCE5-4A17-818A-ED80BE673EBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{DA851ED7-FCE5-4A17-818A-ED80BE673EBB}.Release|Any CPU.Build.0 = Release|Any CPU
2127
EndGlobalSection
2228
EndGlobal

Unicolour/Interpolation.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,28 @@ public static Unicolour InterpolateHsl(this Unicolour startColour, Unicolour end
5959
var a = Interpolate(startAlpha.A, endAlpha.A, distance);
6060
return Unicolour.FromHsl(startColour.Config, h.Modulo(360), s, l, a);
6161
}
62-
62+
6363
private static (double startHue, double endHue) GetHuePoints((bool hasHue, double hueValue) start, (bool hasHue, double hueValue) end)
6464
{
6565
// don't use hue if one colour is monochrome (e.g. black n/a° to green 120° should always stay at hue 120°)
6666
var noHue = !start.hasHue && !end.hasHue;
6767
var startHue = noHue || start.hasHue ? start.hueValue : end.hueValue;
6868
var endHue = noHue || end.hasHue ? end.hueValue : start.hueValue;
69-
70-
var forwardStart = startHue;
71-
var forwardEnd = endHue;
72-
var backwardStart = Math.Min(startHue, endHue) + 360;
73-
var backwardEnd = Math.Max(startHue, endHue);
74-
75-
var interpolateForward = Math.Abs(forwardStart - forwardEnd) <= Math.Abs(backwardStart - backwardEnd);
76-
startHue = interpolateForward ? forwardStart : backwardStart;
77-
endHue = interpolateForward ? forwardEnd : backwardEnd;
78-
69+
70+
if (startHue > endHue)
71+
{
72+
var endViaRed = endHue + 360;
73+
var interpolateViaRed = Math.Abs(startHue - endViaRed) < Math.Abs(startHue - endHue);
74+
return (startHue, interpolateViaRed ? endViaRed : endHue);
75+
}
76+
77+
if (endHue > startHue)
78+
{
79+
var startViaRed = startHue + 360;
80+
var interpolateViaRed = Math.Abs(endHue - startViaRed) < Math.Abs(endHue - startHue);
81+
return (interpolateViaRed ? startViaRed : startHue, endHue);
82+
}
83+
7984
return (startHue, endHue);
8085
}
8186

Unicolour/Unicolour.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<TargetFramework>netstandard2.0</TargetFramework>
1616
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1717
<PackageIcon>Resources\Unicolour.png</PackageIcon>
18-
<PackageVersion>1.1.0</PackageVersion>
18+
<PackageVersion>1.1.1</PackageVersion>
1919
<PackageTags>colour color converter RGB HSB HSV HSL LAB XYZ color-spaces colour-spaces interpolation comparison contrast luminance deltaE</PackageTags>
20-
<PackageReleaseNotes>Add HSL support</PackageReleaseNotes>
20+
<PackageReleaseNotes>Fix hue-based interpolation</PackageReleaseNotes>
2121
<ApplicationIcon>Resources\Unicolour.ico</ApplicationIcon>
2222
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2323
</PropertyGroup>

0 commit comments

Comments
 (0)