-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from happo/yic
Replace euclidean distance with YIC NTSC
- Loading branch information
Showing
9 changed files
with
100 additions
and
31 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const colorDelta = require('../colorDelta'); | ||
|
||
it('is large when comparing black and white', () => { | ||
expect(colorDelta([0, 0, 0, 255], [255, 255, 255, 255])) | ||
.toBeGreaterThan(0.92); | ||
}); | ||
|
||
it('is small when comparing black and very dark grey', () => { | ||
expect(colorDelta([0, 0, 0, 255], [10, 10, 10, 255])) | ||
.toBeLessThan(0.02); | ||
}); | ||
|
||
it('is medium when comparing black and medium grey', () => { | ||
const delta = colorDelta([0, 0, 0, 255], [127, 127, 127, 255]); | ||
expect(delta).toBeGreaterThan(0.21); | ||
expect(delta).toBeLessThan(0.24); | ||
}); | ||
|
||
it('is medium when comparing red and blue', () => { | ||
const delta = colorDelta([255, 0, 0, 255], [0, 0, 255, 255]); | ||
expect(delta).toBeGreaterThan(0.5); | ||
expect(delta).toBeLessThan(0.51); | ||
}); | ||
|
||
it('is zero when comparing transparent and white', () => { | ||
expect(colorDelta([0, 0, 0, 0], [255, 255, 255, 255])) | ||
.toEqual(0); | ||
}); | ||
|
||
it('is large when comparing transparent and black', () => { | ||
expect(colorDelta([0, 0, 0, 0], [0, 0, 0, 255])) | ||
.toBeGreaterThan(0.92); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const MAX_YIQ_DIFFERENCE = 35215; | ||
|
||
function rgb2y(r, g, b) { | ||
return r * 0.29889531 + g * 0.58662247 + b * 0.11448223; | ||
} | ||
|
||
function rgb2i(r, g, b) { | ||
return r * 0.59597799 - g * 0.2741761 - b * 0.32180189; | ||
} | ||
|
||
function rgb2q(r, g, b) { | ||
return r * 0.21147017 - g * 0.52261711 + b * 0.31114694; | ||
} | ||
|
||
// blend semi-transparent color with white | ||
function blend(color, alpha) { | ||
return 255 + (color - 255) * alpha; | ||
} | ||
|
||
// calculate color difference according to the paper "Measuring perceived color | ||
// difference using YIQ NTSC transmission color space in mobile applications" by | ||
// Y. Kotsarenko and F. Ramos | ||
// | ||
// Modified from https://github.com/mapbox/pixelmatch | ||
module.exports = function colorDelta(previousPixel, currentPixel) { | ||
let [r1, g1, b1, a1] = previousPixel; | ||
let [r2, g2, b2, a2] = currentPixel; | ||
|
||
if (r1 === r2 && g1 === g2 && b1 === b2 && a1 === a2) { | ||
return 0; | ||
} | ||
|
||
if (a1 < 255) { | ||
a1 /= 255; | ||
r1 = blend(r1, a1); | ||
g1 = blend(g1, a1); | ||
b1 = blend(b1, a1); | ||
} | ||
|
||
if (a2 < 255) { | ||
a2 /= 255; | ||
r2 = blend(r2, a2); | ||
g2 = blend(g2, a2); | ||
b2 = blend(b2, a2); | ||
} | ||
|
||
const y = rgb2y(r1, g1, b1) - rgb2y(r2, g2, b2); | ||
const i = rgb2i(r1, g1, b1) - rgb2i(r2, g2, b2); | ||
const q = rgb2q(r1, g1, b1) - rgb2q(r2, g2, b2); | ||
|
||
return (0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q) / MAX_YIQ_DIFFERENCE; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters