Skip to content

Fixes Tint, Shade, and Contrast Alpha Interference Issue #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/adjusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ exports.blackness = exports.b = hslwbAdjuster('blackness');
*/

exports.blend = function (color, args) {
var targetAlpha = color.alpha();

// Reset the alpha value to one. This is required because color.mix mixes
// the alpha value as well as rgb values. For blend() purposes, that's not
// what we want.
color.alpha(1);

var other = new Color(args[0].value);
var percentage = 1 - parseInt(args[1].value, 10) / 100;
color.mix(other, percentage);

// Finally set the alpha value of the mixed color to the target value.
color.mix(other, percentage).alpha(targetAlpha);
};

/**
Expand Down Expand Up @@ -79,7 +88,11 @@ exports.contrast = function (color, args) {
var minRatio = 4.5;
if (color.contrast(max) > minRatio) {
var min = binarySearchBWContrast(minRatio, color, max);
min.mix(max, percentage);
var targetMinAlpha = min.alpha();
// Set the alpha to 1 to avoid mix()-ing the alpha value.
min.alpha(1);
// mixes the colors then sets the alpha back to the target alpha.
min.mix(max, percentage).alpha(targetMinAlpha);
}
color.hwb(min.hwb());
};
Expand Down
30 changes: 30 additions & 0 deletions test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,32 @@ describe('#convert', function () {
});
});

describe('tint with alpha', function () {
it('should blend a color with white and adjust the alpha', function () {
convert('red a(40%) tint(50%)', 'rgba(255, 128, 128, 0.4)');
});

it('should blend a color with white and adjust the alpha', function () {
convert('red tint(50%) a(20%)', 'rgba(255, 128, 128, 0.2)');
});
});

describe('shade', function () {
it('should blend a color with black', function () {
convert('red shade(50%)', 'rgb(128, 0, 0)');
});
});

describe('shade with alpha', function () {
it('should blend a color with black and adjust the alpha', function () {
convert('red a(40%) shade(50%)', 'rgba(128, 0, 0, 0.4)');
});

it('should blend a color with black and adjust the alpha', function () {
convert('red shade(50%) a(25%)', 'rgba(128, 0, 0, 0.25)');
});
});

describe('contrast', function () {
it('should darken the same hue with a light color', function () {
convert('hwb(180, 10%, 0%) contrast(0%)', 'rgb(13, 115, 115)'); // hwb(180, 5%, 55%)
Expand All @@ -299,6 +319,16 @@ describe('#convert', function () {
});
});

describe('contrast with alpha', function () {
it('should go to white with a dark color and the given alpha', function() {
convert('black a(40%) contrast(99%)', 'rgba(255, 255, 255, 0.4)');
});

it('should go to black with a light color and the given alpha', function() {
convert('white a(50%) contrast(99%)', 'rgba(0, 0, 0, 0.5)');
});
});

describe('nested color functions', function () {
it('should convert nested color functions', function () {
convert('color(rebeccapurple a(- 10%)) a(- 10%)', 'rgba(102, 51, 153, 0.81)');
Expand Down