Skip to content

Commit b5e52ff

Browse files
committedApr 18, 2020
Merge ColorMine. Refactor script functions to compiled cmdlets
Originally, ColorMine had an external maintainer, but he's taken his work down and sold the source... I'm getting rid of the external library and the namespace. +semver:breaking
1 parent d752801 commit b5e52ff

40 files changed

+2845
-205
lines changed
 

‎Docs/Get-Complement.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
external help file: Pansies-help.xml
33
Module Name: Pansies
4-
online version:
4+
online version:
55
schema: 2.0.0
66
---
77

@@ -14,16 +14,16 @@ Get the Hue complement color
1414
## SYNTAX
1515

1616
```
17-
Get-Complement [-Color] <RgbColor> [-ForceContrast] [-ConsoleColor] [-Passthru]
17+
Get-Complement [-Color] <RgbColor> [-HighContrast] [-BlackAndWhite] [-Passthru]
1818
```
1919

2020
## DESCRIPTION
2121

2222
Returns a color that is 180 degrees opposite around the Hue component of the HSL color space.
2323

24-
The primary reason for Get-Complement to exist is for generating contrasting colors for foreground and background. For that reason, it's usually called with `-ForceContrast` mode, which changes the lightness or darkness to increase the contrast of the returned color.
24+
The primary reason for Get-Complement to exist is for generating contrasting colors for foreground and background. For that reason, it's usually called with `-HighContrast` mode, which changes the lightness or darkness to increase the contrast of the returned color.
2525

26-
It also has a `-ConsoleColor` switch that causes it to assume only 16 colors will work (PowerLine currently uses this mode by default because so many terminals don't support more --including Windows 10 prior to Creators Update). In ConsoleColor mode, it always returns White or Black.
26+
It also has a `-BlackAndWhite` switch that causes it to assume only 16 colors will work (PowerLine currently uses this mode by default because so many terminals don't support more --including Windows 10 prior to Creators Update). In BlackAndWhite mode, it always returns White or Black.
2727

2828
## EXAMPLES
2929

@@ -39,13 +39,13 @@ Gets the color Red back, as the complement for Cyan.
3939
### ---- Example 2 -------------------------------------------------------------
4040

4141
```
42-
PS C:\> $Background, $Foreground = Get-Complement Cyan -Passthru -ConsoleColor
43-
PS C:\> Write-Host " Hello World " -Foreground $Foreground.ConsoleColor -Background $Background.ConsoleColor
42+
PS C:\> $Background, $Foreground = Get-Complement Cyan -Passthru -BlackAndWhite
43+
PS C:\> Microsoft.PowerShell.Utility\Write-Host " Hello World " -Foreground $Foreground.ConsoleColor -Background $Background.ConsoleColor
4444
```
4545

46-
This example shows how using `-Passthru` returns both the original color and the contrasting color, and how using `-ConsoleColor` results in a better contrast when you're being forced to use ConsoleColor (as with the built-in Write-Host command).
46+
This example shows how using `-Passthru` returns both the original color and the contrasting color, and how using `-BlackAndWhite` results in a better contrast when you're being forced to use BlackAndWhite (as with the built-in Write-Host command).
4747

48-
You can try the example without `-ConsoleColor` to see the difference: with it, you'll get Black on Cyan, without, you'll get Red on Cyan. Note that using -ForceContrast will make almost no difference if you're using the `ConsoleColor` property, because downsampling to 16 colors has to result in either Red or DarkRed...
48+
You can try the example without `-BlackAndWhite` to see the difference: with it, you'll get Black on Cyan, without, you'll get Red on Cyan. Note that using -HighContrast will make almost no difference if you're using the `BlackAndWhite` property, because downsampling to 16 colors has to result in either Red or DarkRed...
4949

5050

5151

@@ -58,7 +58,7 @@ The source color to calculate the complement of
5858
```yaml
5959
Type: RgbColor
6060
Parameter Sets: (All)
61-
Aliases:
61+
Aliases:
6262

6363
Required: True
6464
Position: 1
@@ -67,13 +67,14 @@ Accept pipeline input: True (ByValue)
6767
Accept wildcard characters: False
6868
```
6969
70-
### -ForceContrast
70+
### -HighContrast
71+
7172
Force the luminance to have "enough" contrast
7273
7374
```yaml
7475
Type: SwitchParameter
7576
Parameter Sets: (All)
76-
Aliases:
77+
Aliases:
7778

7879
Required: False
7980
Position: Named
@@ -82,13 +83,14 @@ Accept pipeline input: False
8283
Accept wildcard characters: False
8384
```
8485
85-
### -ConsoleColor
86-
Assume there are only 16 colors. Return either black or white to get a readable contrast color.
86+
### -BlackAndWhite
87+
88+
Returns either black or white to get a readable contrast color.
8789
8890
```yaml
8991
Type: SwitchParameter
9092
Parameter Sets: (All)
91-
Aliases:
93+
Aliases:
9294

9395
Required: False
9496
Position: Named
@@ -104,7 +106,7 @@ If set, output the input $Color before the complement
104106
```yaml
105107
Type: SwitchParameter
106108
Parameter Sets: (All)
107-
Aliases:
109+
Aliases:
108110

109111
Required: False
110112
Position: Named

‎Docs/Get-Gradient.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
external help file: Pansies-help.xml
33
Module Name: Pansies
4-
online version:
4+
online version:
55
schema: 2.0.0
66
---
77

@@ -14,7 +14,7 @@ Get a range of colors between two colors
1414
## SYNTAX
1515

1616
```
17-
Get-Gradient [-StartColor] <RgbColor> [-EndColor] <RgbColor> [[-Height] <Int32>] [[-Width] <Int32>] [[-ColorSpace] <Object>] [-Reverse] [-Flatten]
17+
Get-Gradient [-StartColor] <RgbColor> [-EndColor] <RgbColor> [[-Width] <Int32>] [[-Height] <Int32>] [[-ColorSpace] <Object>] [-Reverse] [-Flatten]
1818
```
1919

2020
## DESCRIPTION
@@ -38,19 +38,18 @@ Gets a simple array of ten colors between Red and Blue.
3838
PS C:\> Get-Gradient Red Blue
3939
```
4040

41-
Gets a 2D gradient from the ConsoleColor Red to Blue, with the width x height the current size of the console.
42-
43-
41+
Gets a 2D gradient from the ConsoleColor Red to Blue, with as many colors as the current width of the console.
4442

4543
## PARAMETERS
4644

4745
### -StartColor
46+
4847
The left color to start the gradient from.
4948

5049
```yaml
5150
Type: RgbColor
5251
Parameter Sets: (All)
53-
Aliases:
52+
Aliases:
5453

5554
Required: True
5655
Position: 1
@@ -67,7 +66,7 @@ The right color to end the gradient at.
6766
```yaml
6867
Type: RgbColor
6968
Parameter Sets: (All)
70-
Aliases:
69+
Aliases:
7170

7271
Required: True
7372
Position: 2
@@ -77,6 +76,7 @@ Accept wildcard characters: False
7776
```
7877
7978
### -Width
79+
8080
The number of columns to generate in the gradient. Defaults to the width of the console.
8181
8282
```yaml
@@ -92,12 +92,13 @@ Accept wildcard characters: False
9292
```
9393
9494
### -Height
95+
9596
The number of rows to generate in the gradient. Defaults to 1
9697
9798
```yaml
9899
Type: Int32
99100
Parameter Sets: (All)
100-
Aliases:
101+
Aliases:
101102

102103
Required: False
103104
Position: 4
@@ -107,13 +108,14 @@ Accept wildcard characters: False
107108
```
108109
109110
### -ColorSpace
110-
A color space to render the gradient in. Defaults to HunterLab, but can be any of
111-
CMY, CMYK, LAB, LCH, LUV, HunterLAB, HSL, HSV, HSB, RGB, XYZ, YXY
111+
A color space to render the gradient in. Defaults to HunterLab, which
112+
generates gradients that are very consistent in their brightness,
113+
but can be any of CMY, CMYK, LAB, LCH, LUV, HunterLAB, HSL, HSV, HSB, RGB, XYZ, YXY
112114
113115
```yaml
114116
Type: Object
115117
Parameter Sets: (All)
116-
Aliases:
118+
Aliases:
117119

118120
Required: False
119121
Position: Named
@@ -128,7 +130,7 @@ For color spaces with Hue (HSL, HSV, HSB), setting this generates the gradient t
128130
```yaml
129131
Type: SwitchParameter
130132
Parameter Sets: (All)
131-
Aliases:
133+
Aliases:
132134

133135
Required: False
134136
Position: Named
@@ -143,7 +145,7 @@ Flattens the 2D array to a single array.
143145
```yaml
144146
Type: SwitchParameter
145147
Parameter Sets: (All)
146-
Aliases:
148+
Aliases:
147149

148150
Required: False
149151
Position: Named

‎Pansies.csproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
<Generator>TextTemplatingFileGenerator</Generator>
1616
<LastGenOutput>Colors.cs</LastGenOutput>
1717
</None>
18+
<Compile Update="Source\Assembly\ColorSpaces\ColorSpaces.cs">
19+
<DependentUpon>ColorSpaces.tt</DependentUpon>
20+
<DesignTime>True</DesignTime>
21+
<AutoGen>True</AutoGen>
22+
</Compile>
23+
<None Update="Source\Assembly\ColorSpaces\ColorSpaces.tt">
24+
<LastGenOutput>ColorSpaces.cs</LastGenOutput>
25+
<Generator>TextTemplatingFileGenerator</Generator>
26+
</None>
1827
</ItemGroup>
1928
<ItemGroup>
2029
<!-- I wouldn't want to double-compile the stuff in here -->
@@ -28,7 +37,6 @@
2837
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-04" />
2938
</ItemGroup>
3039
<ItemGroup>
31-
<ProjectReference Include="lib\ColorMine\ColorMine\ColorMine.csproj" />
3240
<ProjectReference Include="lib\p2f\src\CodeOwls.PowerShell\CodeOwls.PowerShell.Paths\CodeOwls.PowerShell.Paths.csproj" />
3341
<ProjectReference Include="lib\p2f\src\CodeOwls.PowerShell\CodeOwls.PowerShell.Provider\CodeOwls.PowerShell.Provider.csproj" />
3442
</ItemGroup>

‎Pansies.sln

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

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27703.2000
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29806.167
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pansies", "Pansies.csproj", "{A70FB8C2-26DB-45A8-AEFC-CC3496AF1FAD}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ColorMine", "lib\ColorMine\ColorMine\ColorMine.csproj", "{25D4CBB8-B010-4768-95D5-F21B54547FB6}"
9-
EndProject
108
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeOwls.PowerShell.Paths", "lib\p2f\src\CodeOwls.PowerShell\CodeOwls.PowerShell.Paths\CodeOwls.PowerShell.Paths.csproj", "{F9F6FA51-1579-4F16-8F5E-3CE16FAA9B43}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeOwls.PowerShell.Provider", "lib\p2f\src\CodeOwls.PowerShell\CodeOwls.PowerShell.Provider\CodeOwls.PowerShell.Provider.csproj", "{3D3741C8-A7A9-4532-A84A-0E7C90214800}"
@@ -21,10 +19,6 @@ Global
2119
{A70FB8C2-26DB-45A8-AEFC-CC3496AF1FAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2220
{A70FB8C2-26DB-45A8-AEFC-CC3496AF1FAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2321
{A70FB8C2-26DB-45A8-AEFC-CC3496AF1FAD}.Release|Any CPU.Build.0 = Release|Any CPU
24-
{25D4CBB8-B010-4768-95D5-F21B54547FB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{25D4CBB8-B010-4768-95D5-F21B54547FB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{25D4CBB8-B010-4768-95D5-F21B54547FB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
27-
{25D4CBB8-B010-4768-95D5-F21B54547FB6}.Release|Any CPU.Build.0 = Release|Any CPU
2822
{F9F6FA51-1579-4F16-8F5E-3CE16FAA9B43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2923
{F9F6FA51-1579-4F16-8F5E-3CE16FAA9B43}.Debug|Any CPU.Build.0 = Debug|Any CPU
3024
{F9F6FA51-1579-4F16-8F5E-3CE16FAA9B43}.Release|Any CPU.ActiveCfg = Release|Any CPU
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using PoshCode.Pansies.ColorSpaces.Comparisons;
2+
3+
namespace PoshCode.Pansies.ColorSpaces
4+
{
5+
public delegate double ComparisonAlgorithm(IColorSpace a, IColorSpace b);
6+
7+
/// <summary>
8+
/// Defines the public methods for all color spaces
9+
/// </summary>
10+
public interface IColorSpace
11+
{
12+
/// <summary>
13+
/// Initialize settings from an Rgb object
14+
/// </summary>
15+
/// <param name="color"></param>
16+
void Initialize(IRgb color);
17+
18+
/// <summary>
19+
/// Convert the color space to Rgb, you should probably using the "To" method instead. Need to figure out a way to "hide" or otherwise remove this method from the public interface.
20+
/// </summary>
21+
/// <returns></returns>
22+
IRgb ToRgb();
23+
24+
/// <summary>
25+
/// Convert any IColorSpace to any other IColorSpace.
26+
/// </summary>
27+
/// <typeparam name="T">IColorSpace type to convert to</typeparam>
28+
/// <returns></returns>
29+
T To<T>() where T : IColorSpace, new();
30+
31+
/// <summary>
32+
/// Determine how close two IColorSpaces are to each other using a passed in algorithm
33+
/// </summary>
34+
/// <param name="compareToValue">Other IColorSpace to compare to</param>
35+
/// <param name="comparer">Algorithm to use for comparison</param>
36+
/// <returns>Distance in 3d space as double</returns>
37+
double Compare(IColorSpace compareToValue, IColorSpaceComparison comparer);
38+
39+
/// <summary>
40+
/// Array of signifigant values in a consistent order. Useful for generic n-dimensional math.
41+
/// </summary>
42+
double[] Ordinals { get; set; }
43+
}
44+
45+
/// <summary>
46+
/// Abstract ColorSpace class, defines the To method that converts between any IColorSpace.
47+
/// </summary>
48+
public abstract class ColorSpace : IColorSpace
49+
{
50+
public abstract void Initialize(IRgb color);
51+
public abstract IRgb ToRgb();
52+
public abstract double[] Ordinals { get; set; }
53+
54+
/// <summary>
55+
/// Convienience method for comparing any IColorSpace
56+
/// </summary>
57+
/// <param name="compareToValue"></param>
58+
/// <param name="comparer"></param>
59+
/// <returns>Single number representing the difference between two colors</returns>
60+
public double Compare(IColorSpace compareToValue, IColorSpaceComparison comparer)
61+
{
62+
return comparer.Compare(this, compareToValue);
63+
}
64+
65+
/// <summary>
66+
/// Convert any IColorSpace to any other IColorSpace
67+
/// </summary>
68+
/// <typeparam name="T">Must implement IColorSpace, new()</typeparam>
69+
/// <returns></returns>
70+
public T To<T>() where T : IColorSpace, new()
71+
{
72+
if (typeof(T) == GetType())
73+
{
74+
return (T)MemberwiseClone();
75+
}
76+
77+
var newColorSpace = new T();
78+
newColorSpace.Initialize(ToRgb());
79+
80+
return newColorSpace;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)