Skip to content

Commit

Permalink
Fixes RGB to/from YIQ conversion to use gamma-corrected RGB instead o…
Browse files Browse the repository at this point in the history
…f linear, fixes #185

Updated docs in regards to sRGB/YIQ conversion
  • Loading branch information
danburzo committed Jan 24, 2023
1 parent daa1213 commit 6b8ba09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions docs/color-spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ Serialized as `color(--jzczhz j c h)`, with the `none` keyword for any missing c

Serialized as `color(--yiq y i q)`, with the `none` keyword for any missing color channel. An explicit `alpha < 1` is included as ` / alpha`.

The conversion matrices between the sRGB and YIQ color spaces are taken from:

> Yuriy Kotsarenko, Fernando Ramos, [_Measuring perceived color difference using YIQ NTSC transmission color space in mobile applications_](http://www.progmat.uaem.mx:8080/artVol2Num2/Articulo3Vol2Num2.pdf), Programación Matemática y Software (2010), Vol. 2, No 2.
### CIE XYZ

The [CIE XYZ color space](https://en.wikipedia.org/wiki/CIE_1931_color_space), also known as the CIE 1931 color space.
Expand Down
7 changes: 2 additions & 5 deletions src/yiq/convertRgbToYiq.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import convertRgbToLrgb from '../lrgb/convertRgbToLrgb.js';

const convertRgbToYiq = rgb => {
let { r, g, b, alpha } = convertRgbToLrgb(rgb);
let res = {
const convertRgbToYiq = ({ r, g, b, alpha }) => {
const res = {
mode: 'yiq',
y: 0.29889531 * r + 0.58662247 * g + 0.11448223 * b,
i: 0.59597799 * r - 0.2741761 * g - 0.32180189 * b,
Expand Down
15 changes: 8 additions & 7 deletions src/yiq/convertYiqToRgb.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import convertLrgbToRgb from '../lrgb/convertLrgbToRgb.js';

const convertYiqToRgb = ({ y, i, q, alpha }) =>
convertLrgbToRgb({
const convertYiqToRgb = ({ y, i, q, alpha }) => {
const res = {
mode: 'rgb',
r: y + 0.95608445 * i + 0.6208885 * q,
g: y - 0.27137664 * i - 0.6486059 * q,
b: y - 1.10561724 * i + 1.70250126 * q,
alpha
});
b: y - 1.10561724 * i + 1.70250126 * q
};
if (alpha !== undefined) res.alpha = alpha;
return res;
};

export default convertYiqToRgb;
27 changes: 17 additions & 10 deletions test/yiq.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import tape from 'tape';
import { yiq, formatRgb, formatCss } from '../src/index.js';

tape('rgb to yiq and back', function (test) {
test.deepEqual(
tape('rgb -> yiq', t => {
t.deepEqual(yiq('purple'), {
mode: 'yiq',
y: 0.20749931419607845,
i: 0.13762565019607842,
q: 0.26233329443137254
});
t.end();
});

tape('rgb -> yiq -> rgb', t => {
t.deepEqual(
formatRgb(yiq('rgb(255, 255, 255)')),
'rgb(255, 255, 255)',
'white'
);

test.deepEqual(formatRgb(yiq('rgb(0, 0, 0)')), 'rgb(0, 0, 0)', 'black');
t.deepEqual(formatRgb(yiq('rgb(0, 0, 0)')), 'rgb(0, 0, 0)', 'black');
t.deepEqual(formatRgb(yiq('rgb(100, 0, 0)')), 'rgb(100, 0, 0)', 'red');
t.deepEqual(formatRgb(yiq('rgb(0, 120, 0)')), 'rgb(0, 120, 0)', 'blue');
t.deepEqual(formatRgb(yiq('rgb(0, 0, 89)')), 'rgb(0, 0, 89)', 'green');

test.deepEqual(formatRgb(yiq('rgb(100, 0, 0)')), 'rgb(100, 0, 0)', 'red');

test.deepEqual(formatRgb(yiq('rgb(0, 120, 0)')), 'rgb(0, 120, 0)', 'blue');

test.deepEqual(formatRgb(yiq('rgb(0, 0, 89)')), 'rgb(0, 0, 89)', 'green');

test.end();
t.end();
});

tape('color(--yiq)', t => {
Expand Down

0 comments on commit 6b8ba09

Please sign in to comment.