Skip to content

Commit 00332ac

Browse files
committed
Add YIQ color space
1 parent 0c47497 commit 00332ac

File tree

6 files changed

+191
-5
lines changed

6 files changed

+191
-5
lines changed

ColorHelper/Color/ColorNames.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,5 +2840,10 @@ public static XYZ ToXyz(this ColorName colorName)
28402840
{
28412841
return ColorConverter.RgbToXyz(colorName.ToRgb());
28422842
}
2843+
2844+
public static YIQ ToYiq(this ColorName colorName)
2845+
{
2846+
return ColorConverter.RgbToYiq(colorName.ToRgb());
2847+
}
28432848
}
28442849
}

ColorHelper/Color/YIQ.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace ColorHelper
2+
{
3+
public class YIQ : IColor
4+
{
5+
public double Y { get; set; }
6+
public double I { get; set; }
7+
public double Q { get; set; }
8+
9+
public YIQ(double y, double i, double q)
10+
{
11+
this.Y = y;
12+
this.I = i;
13+
this.Q = q;
14+
}
15+
16+
public override bool Equals(object obj)
17+
{
18+
var result = (YIQ)obj;
19+
20+
return (
21+
result != null &&
22+
this.Y == result.Y &&
23+
this.I == result.I &&
24+
this.Q == result.Q);
25+
}
26+
27+
public override string ToString()
28+
{
29+
return $"{this.Y} {this.I} {this.Q}";
30+
}
31+
}
32+
}

ColorHelper/ColorHelper.csproj

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Authors>Artyom Gritsuk</Authors>
66
<RepositoryUrl>https://github.com/iamartyom/ColorHelper</RepositoryUrl>
77
<PackageProjectUrl>https://github.com/iamartyom/ColorHelper</PackageProjectUrl>
8-
<PackageTags>Color, Converter, RGB, HEX, CMYK, HSV, HSL, XYZ</PackageTags>
9-
<PackageReleaseNotes>Fix issue of black not being returned correctly for HSV HSL and another minor changes.</PackageReleaseNotes>
8+
<PackageTags>Color, Converter, RGB, HEX, CMYK, HSV, HSL, XYZ, YIQ</PackageTags>
9+
<PackageReleaseNotes>Add YIQ color space</PackageReleaseNotes>
1010
<Description>This library contains several useful classes:
1111

1212
- ColorConverter
@@ -16,31 +16,43 @@ RgbToCmyk
1616
RgbToHsv
1717
RgbToHsl
1818
RgbToXyz
19+
RgbToYiq
1920
HexToRgb
2021
HexToCmyk
2122
HexToHsv
2223
HexToHsl
2324
HexToXyz
25+
HexToYiq
2426
CmykToRgb
2527
CmykToHex
2628
CmykToHsv
2729
CmykToHsl
2830
CmykToXyz
31+
CmykToYiq
2932
HsvToRgb
3033
HsvToHex
3134
HsvToCmyk
3235
HsvToHsl
3336
HsvToXyz
37+
HsvToYiq
3438
HslToRgb
3539
HslToHex
3640
HslToCmyk
3741
HslToHsv
3842
HslToXyz
43+
HslToYiq
3944
XyzToRgb
4045
XyzToHex
4146
XyzToCmyk
4247
XyzToHsv
4348
XyzToHsl
49+
XyzToYiq
50+
YiqToRgb
51+
YiqToHex
52+
YiqToCmyk
53+
YiqToHsv
54+
YiqToHsl
55+
YiqToXyz
4456

4557
- ColorComparer
4658

@@ -55,9 +67,9 @@ Enums
5567
- ColorName</Description>
5668
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5769
<PackageLicenseExpression>MIT</PackageLicenseExpression>
58-
<Version>1.6.2</Version>
70+
<Version>1.7.0</Version>
5971
<PackageIcon>logo.jpg</PackageIcon>
60-
<PackageVersion>1.6.2</PackageVersion>
72+
<PackageVersion>1.7.0</PackageVersion>
6173
</PropertyGroup>
6274

6375
<ItemGroup>

ColorHelper/Converter/ColorConverter.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public static XYZ RgbToXyz(RGB rgb)
101101
);
102102
}
103103

104+
public static YIQ RgbToYiq(RGB rgb)
105+
{
106+
double[] modifiedRGB = { rgb.R / 255.0, rgb.G / 255.0, rgb.B / 255.0 };
107+
108+
return new YIQ(
109+
(modifiedRGB[0] * 0.299 + modifiedRGB[1] * 0.587 + modifiedRGB[2] * 0.114),
110+
(modifiedRGB[0] * 0.596 - modifiedRGB[1] * 0.274 - modifiedRGB[2] * 0.322),
111+
(modifiedRGB[0] * 0.211 - modifiedRGB[1] * 0.522 + modifiedRGB[2] * 0.311)
112+
);
113+
}
114+
104115
public static RGB HexToRgb(HEX hex)
105116
{
106117
int value = Convert.ToInt32(hex.Value, 16);
@@ -130,6 +141,11 @@ public static XYZ HexToXyz(HEX hex)
130141
return RgbToXyz(HexToRgb(hex));
131142
}
132143

144+
public static YIQ HexToYiq(HEX hex)
145+
{
146+
return RgbToYiq(HexToRgb(hex));
147+
}
148+
133149
public static RGB CmykToRgb(CMYK cmyk)
134150
{
135151
return new RGB(
@@ -191,6 +207,11 @@ public static XYZ HsvToXyz(HSV hsv)
191207
return RgbToXyz(HsvToRgb(hsv));
192208
}
193209

210+
public static YIQ HsvToYiq(HSV hsv)
211+
{
212+
return RgbToYiq(HsvToRgb(hsv));
213+
}
214+
194215
public static RGB HslToRgb(HSL hsl)
195216
{
196217
double modifiedH, modifiedS, modifiedL, r = 1, g = 1, b = 1, q, p;
@@ -269,6 +290,11 @@ public static XYZ HslToXyz(HSL hsl)
269290
return RgbToXyz(HslToRgb(hsl));
270291
}
271292

293+
public static YIQ HslToYiq(HSL hsl)
294+
{
295+
return RgbToYiq(HslToRgb(hsl));
296+
}
297+
272298
public static RGB XyzToRgb(XYZ xyz)
273299
{
274300
double modifiedX = xyz.X / 100.0, modifiedY = xyz.Y / 100.0, modifiedZ = xyz.Z / 100.0;
@@ -305,5 +331,45 @@ public static HSL XyzToHsl(XYZ xyz)
305331
{
306332
return RgbToHsl(XyzToRgb(xyz));
307333
}
334+
335+
public static YIQ XyzToYiq(XYZ xyz)
336+
{
337+
return RgbToYiq(XyzToRgb(xyz));
338+
}
339+
340+
public static RGB YiqToRgb(YIQ yiq)
341+
{
342+
double[] rgb = new double[3];
343+
rgb[0] = yiq.Y + yiq.I * 0.956 + yiq.Q * 0.621;
344+
rgb[1] = yiq.Y - yiq.I * 0.272 - yiq.Q * 0.647;
345+
rgb[2] = yiq.Y - yiq.I * 1.106 + yiq.Q * 1.703;
346+
347+
return new RGB((byte)Math.Round(rgb[0] * 255), (byte)Math.Round(rgb[1] * 255), (byte)Math.Round(rgb[2] * 255));
348+
}
349+
350+
public static HEX YiqToHex(YIQ yiq)
351+
{
352+
return RgbToHex(YiqToRgb(yiq));
353+
}
354+
355+
public static CMYK YiqToCmyk(YIQ yiq)
356+
{
357+
return RgbToCmyk(YiqToRgb(yiq));
358+
}
359+
360+
public static HSV YiqToHsv(YIQ yiq)
361+
{
362+
return RgbToHsv(YiqToRgb(yiq));
363+
}
364+
365+
public static HSL YiqToHsl(YIQ yiq)
366+
{
367+
return RgbToHsl(YiqToRgb(yiq));
368+
}
369+
370+
public static XYZ YiqToXyz(YIQ yiq)
371+
{
372+
return RgbToXyz(YiqToRgb(yiq));
373+
}
308374
}
309375
}

ColorHelper/Generator/ColorGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ private static T ConvertRgbToNecessaryColorType<T>(RGB rgb) where T: IColor
7171
{
7272
return (T)(object)ColorConverter.RgbToXyz(rgb);
7373
}
74+
else if (typeof(T) == typeof(YIQ))
75+
{
76+
return (T)(object)ColorConverter.RgbToYiq(rgb);
77+
}
7478
else
7579
{
7680
throw new ArgumentException("Incorrect class type");

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Supported color spaces:
77
- HSV
88
- HSL
99
- XYZ
10+
- YIQ
1011

1112
This library provides several useful classes:
1213

@@ -37,6 +38,11 @@ Convert Rgb to Xyz:
3738
XYZ xyz = ColorConverter.RgbToXyz(new RGB(10, 20, 30));
3839
```
3940

41+
Convert Rgb to Yiq:
42+
```
43+
YIQ yiq = ColorConverter.RgbToYiq(new RGB(10, 20, 30));
44+
```
45+
4046
Convert Hex to Rgb:
4147
```
4248
RGB rgb = ColorConverter.HexToRgb(new HEX("#00FF00"));
@@ -62,6 +68,11 @@ Convert Hex to Xyz:
6268
XYZ xyz = ColorConverter.HexToXyz(new HEX("#00FF00"));
6369
```
6470

71+
Convert Hex to Yiq:
72+
```
73+
YIQ yiq = ColorConverter.HexToYiq(new HEX("#00FF00"));
74+
```
75+
6576
Convert Cmyk to Rgb:
6677
```
6778
RGB rgb = ColorConverter.CmykToRgb(new CMYK(0, 100, 0, 100));
@@ -87,6 +98,11 @@ Convert Cmyk to Xyz:
8798
XYZ xyz = ColorConverter.CmykToXyz(new CMYK(0, 100, 0, 100));
8899
```
89100

101+
Convert Cmyk to Yiq:
102+
```
103+
YIQ yiq = ColorConverter.CmykToYiq(new CMYK(0, 100, 0, 100));
104+
```
105+
90106
Convert Hsv to Rgb:
91107
```
92108
RGB rgb = ColorConverter.HsvToRgb(new HSL(0, 0, 100));
@@ -112,6 +128,11 @@ Convert Hsv to Xyz:
112128
XYZ xyz = ColorConverter.HsvToXyz(new HSL(0, 0, 100));
113129
```
114130

131+
Convert Hsv to Yiq:
132+
```
133+
YIQ yiq = ColorConverter.HsvToYiq(new HSL(0, 0, 100));
134+
```
135+
115136
Convert Hsl to Rgb:
116137
```
117138
RGB rgb = ColorConverter.HslToRgb(new HSL(0, 0, 100));
@@ -137,6 +158,11 @@ Convert Hsl to Xyz:
137158
XYZ xyz = ColorConverter.HslToXyz(new HSL(0, 0, 100));
138159
```
139160

161+
Convert Hsl to Yiq:
162+
```
163+
YIQ yiq = ColorConverter.HslToYiq(new HSL(0, 0, 100));
164+
```
165+
140166
Convert Xyz to Rgb:
141167
```
142168
RGB rgb = ColorConverter.XyzToRgb(new XYZ(0, 0, 10));
@@ -162,6 +188,45 @@ Convert Xyz to Hsl:
162188
HSL hsl = ColorConverter.XyzToHsl(new XYZ(0, 0, 10));
163189
```
164190

191+
Convert Xyz to Yiq:
192+
```
193+
YIQ yiq = ColorConverter.XyzToYiq(new XYZ(0, 0, 10));
194+
```
195+
196+
197+
198+
199+
200+
Convert Yiq to Rgb:
201+
```
202+
RGB rgb = ColorConverter.YiqToRgb(new YIQ(0.1, 0.1, 0.2));
203+
```
204+
205+
Convert Yiq to Hex:
206+
```
207+
HEX hex = ColorConverter.YiqToHex(new YIQ(0.1, 0.1, 0.2));
208+
```
209+
210+
Convert Yiq to Cmyk:
211+
```
212+
CMYK cmyk = ColorConverter.YiqToCmyk(new YIQ(0.1, 0.1, 0.2));
213+
```
214+
215+
Convert Yiq to Hsv:
216+
```
217+
HSV hsv = ColorConverter.YiqToHsv(new YIQ(0.1, 0.1, 0.2));
218+
```
219+
220+
Convert Yiq to Hsl:
221+
```
222+
HSL hsl = ColorConverter.YiqToHsl(new YIQ(0.1, 0.1, 0.2));
223+
```
224+
225+
Convert Yiq to Xyz:
226+
```
227+
YIQ yiq = ColorConverter.YiqToXyz(new YIQ(0.1, 0.1, 0.2));
228+
```
229+
165230
## ColorComparer
166231
```
167232
bool result = ColorComparer.Equals(new RGB(100, 100, 100), new HEX("#FFFFFF"));
@@ -182,6 +247,7 @@ CMYK cmyk = ColorGenerator.GetRandomColor<CMYK>();
182247
HSV hsv = ColorGenerator.GetRandomColor<HSV>();
183248
HSL hsl = ColorGenerator.GetRandomColor<HSL>();
184249
XYZ xyz = ColorGenerator.GetRandomColor<XYZ>();
250+
YIQ yiq = ColorGenerator.GetRandomColor<YIQ>();
185251
```
186252

187253
Get light random color:
@@ -208,4 +274,5 @@ CMYK cmyk = ColorName.SpanishRed.ToCmyk();
208274
HSV hsv = ColorName.Coral.ToHsv();
209275
HSL hsl = ColorName.NeonFuchsia.ToHsl();
210276
XYZ xyz = ColorName.Raspberry.ToXyz();
277+
YIQ yiq = ColorName.Coffee.ToYiq();
211278
```

0 commit comments

Comments
 (0)