Skip to content

Commit

Permalink
Merge a3cdd24 into 541ddeb
Browse files Browse the repository at this point in the history
  • Loading branch information
mass committed Feb 6, 2014
2 parents 541ddeb + a3cdd24 commit e2e93f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
27 changes: 23 additions & 4 deletions spectra.js
Expand Up @@ -589,10 +589,29 @@
return color1 === color2;
}

return color1.red() === color2.red() &&
color1.green() === color2.green() &&
color1.blue() === color2.blue() &&
color1.alpha() === color2.alpha();
return color1.near(color2, 0);
};

/**
* Tests to see if an other color is within a percentage range of this color.
*/
Spectra.fn.prototype.near = function(other, percentage) {
var color1 = this;
var color2 = other;

if (arguments.length < 2) {
return color1.red() === color2.red() &&
color1.green() === color2.green() &&
color1.blue() === color2.blue() &&
color1.alpha() === color2.alpha();
}

percentage = Math.abs(percentage);
var adjustment = 255 * (percentage / 100);

return (Math.abs(color2.red() - color1.red()) <= adjustment) &&
(Math.abs(color2.green() - color1.green()) <= adjustment) &&
(Math.abs(color2.blue() - color1.blue()) <= adjustment);
};

/**
Expand Down
16 changes: 16 additions & 0 deletions test/tester.js
Expand Up @@ -151,6 +151,22 @@ describe('Spectra', function() {
});

describe('Color operations', function() {
it('Near', function() {
expect(color.near(color)).toBe(true);
expect(color.near(color, 0)).toBe(true);
expect(color.near(color, 100)).toBe(true);
expect(color.near(color, -50)).toBe(true);

color1 = Spectra({r: 0, g: 25, b: 75, a: 1.0});
expect(color.near(color1)).toBe(false);
expect(color.near(color1, 100)).toBe(true);
expect(color.near(color1, 99)).toBe(false);

color1 = Spectra({r: 225, g: 0, b: 50, a: 1.0});
expect(color.near(color1, 0)).toBe(false);
expect(color.near(color1, 12)).toBe(true);
});

it('Color operations', function() {
expect(color.complement().hex()).toBe('#19ffcd');
expect(color.negate().hex()).toBe('#00e6b4');
Expand Down

0 comments on commit e2e93f9

Please sign in to comment.