Skip to content

Commit f145f0b

Browse files
Merge pull request #3 from IvyCafe/lemon73/support-color-type-selector
feat: support color-type selection
2 parents 9cb4b78 + d12311f commit f145f0b

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ Arch:
2121
(To Be Write)
2222
-->
2323

24+
## Usage
25+
26+
```sh
27+
medicolor <image-path>
28+
29+
# e.g.
30+
# medicolor ./sample.png
31+
32+
# then, select color-type in your console
33+
# (output file name is "./output.png")
34+
```
35+
36+
or
37+
38+
```sh
39+
medicolor <image-path> <output-color-type>
40+
# <output-color-type>: (1 = Protanomaly, 2 = Deuteranomaly, 3 = Tritanomaly)
41+
42+
# e.g.
43+
# medicolor ./sample 2
44+
```
45+
2446
## Acknowledgments
2547

2648
Super Thanks:

src/Program.cs

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,42 @@ static int Main(string[] args)
4646
using SKSurface surface = SKSurface.Create(info);
4747
SKCanvas canvas = surface.Canvas;
4848

49-
float[] colorMatrixTritanomaly10 =
50-
[
51-
1.255528f, -0.076749f, -0.178779f, 0, 0,
52-
-0.078411f, 0.930809f, 0.147602f, 0, 0,
53-
0.004733f, 0.691367f, 0.303900f, 0, 0,
54-
0, 0, 0, 1, 0
55-
];
49+
// Check color type
50+
string? typedNumber;
51+
if (args.Length > 1)
52+
{
53+
typedNumber = args[1];
54+
}
55+
else
56+
{
57+
Console.WriteLine("Which type would you like to convert? (1 = Protanomaly, 2 = Deuteranomaly, 3 = Tritanomaly)");
58+
Console.Write("Type the number here: ");
59+
typedNumber = Console.ReadLine();
60+
}
61+
62+
// check the input is number or not
63+
if (!int.TryParse(typedNumber, out int returnNumber))
64+
{
65+
Console.ForegroundColor = ConsoleColor.Red;
66+
Console.WriteLine($"Error: It can't convert {typedNumber} into number.");
67+
Console.ResetColor();
68+
return 1;
69+
}
70+
71+
// check the number is in range or not
72+
float[] colorMatrix = ColorMatrix(returnNumber);
73+
if (colorMatrix.Length == 0)
74+
{
75+
Console.ForegroundColor = ConsoleColor.Red;
76+
Console.WriteLine($"Error: Invalid color type {returnNumber}.");
77+
Console.ResetColor();
78+
return 1;
79+
}
80+
// end of check color type
5681

5782
using (SKPaint paint = new())
5883
{
59-
paint.ColorFilter = SKColorFilter.CreateColorMatrix(colorMatrixTritanomaly10);
84+
paint.ColorFilter = SKColorFilter.CreateColorMatrix(colorMatrix);
6085
canvas.DrawBitmap(bitmap, new SKRect(0, 0, source.Width, source.Height), paint);
6186
}
6287

@@ -69,4 +94,42 @@ static int Main(string[] args)
6994
Console.ResetColor();
7095
return 0;
7196
}
97+
98+
/// <summary>
99+
/// The method to create color-matrix
100+
/// </summary>
101+
/// <param name="colorType">1 = Protanomaly, 2 = Deuteranomaly, 3 = Tritanomaly</param>
102+
/// <returns>Matrix type</returns>
103+
static float[] ColorMatrix(int colorType)
104+
{
105+
switch (colorType)
106+
{
107+
case 1:
108+
return [
109+
0.152286f, 1.052583f, -0.204868f, 0, 0,
110+
0.114503f, 0.786281f, 0.099216f, 0, 0,
111+
-0.003882f, -0.048116f, 1.051998f, 0, 0,
112+
0, 0, 0, 1, 0
113+
];
114+
115+
case 2:
116+
return [
117+
0.367322f, 0.860646f, -0.227968f, 0, 0,
118+
0.280085f, 0.672501f, 0.047413f, 0, 0,
119+
-0.011820f, 0.042940f, 0.968881f, 0, 0,
120+
0, 0, 0, 1, 0
121+
];
122+
123+
case 3:
124+
return [
125+
1.255528f, -0.076749f, -0.178779f, 0, 0,
126+
-0.078411f, 0.930809f, 0.147602f, 0, 0,
127+
0.004733f, 0.691367f, 0.303900f, 0, 0,
128+
0, 0, 0, 1, 0
129+
];
130+
131+
default:
132+
return [];
133+
}
134+
}
72135
}

0 commit comments

Comments
 (0)