Invert Color#gray component.

This commit is contained in:
Jürg Lehni 2013-04-08 20:20:47 -07:00
parent 727db4c5f2
commit ca1a395dbd
2 changed files with 21 additions and 21 deletions

View file

@ -178,21 +178,20 @@ var Color = this.Color = Base.extend(new function() {
// Using the standard NTSC conversion formula that is used for // Using the standard NTSC conversion formula that is used for
// calculating the effective luminance of an RGB color: // calculating the effective luminance of an RGB color:
// http://www.mathworks.com/support/solutions/en/data/1-1ASCU/index.html?solution=1-1ASCU // http://www.mathworks.com/support/solutions/en/data/1-1ASCU/index.html?solution=1-1ASCU
return [1 - (r * 0.2989 + g * 0.587 + b * 0.114)]; return [r * 0.2989 + g * 0.587 + b * 0.114];
}, },
'gray-rgb': function(g) { 'gray-rgb': function(g) {
var comp = 1 - g; return [g, g, g];
return [comp, comp, comp];
}, },
'gray-hsb': function(g) { 'gray-hsb': function(g) {
return [0, 0, 1 - g]; return [0, 0, g];
}, },
'gray-hsl': function(g) { 'gray-hsl': function(g) {
// TODO: Is lightness really the same as brightness for gray? // TODO: Is lightness really the same as brightness for gray?
return [0, 0, 1 - g]; return [0, 0, g];
} }
}; };
@ -310,7 +309,7 @@ var Color = this.Color = Base.extend(new function() {
} }
// Default fallbacks: rgb, black // Default fallbacks: rgb, black
this._type = type || 'rgb'; this._type = type || 'rgb';
this._components = components || (type === 'gray' ? [1] : [0, 0, 0]); this._components = components || (type === 'gray' ? [0] : [0, 0, 0]);
this._alpha = alpha; this._alpha = alpha;
if (this._read) if (this._read)
this._read = read; this._read = read;

View file

@ -40,7 +40,7 @@ test('Set color to object', function() {
var path = new Path(); var path = new Path();
path.fillColor = { gray: 0.2 }; path.fillColor = { gray: 0.2 };
compareColors(path.fillColor, new Color(0.2)); compareColors(path.fillColor, new Color(0.2));
equals(path.fillColor.toCss(), 'rgb(204, 204, 204)'); equals(path.fillColor.toCss(), 'rgb(51, 51, 51)');
}); });
test('Set color to array', function() { test('Set color to array', function() {
@ -73,15 +73,15 @@ test('Creating colors', function() {
test('Get gray from RGB Color', function() { test('Get gray from RGB Color', function() {
var color = new Color(1, 0.5, 0.2); var color = new Color(1, 0.5, 0.2);
compareNumbers(color.gray, 0.3848); compareNumbers(color.gray, 0.6152);
var color = new Color(0.5, 0.2, 0.1); var color = new Color(0.5, 0.2, 0.1);
compareNumbers(color.gray, 0.72175); compareNumbers(color.gray, 0.27825);
}); });
test('Get gray from HSB Color', function() { test('Get gray from HSB Color', function() {
var color = new HsbColor(0, 0, 0.2); var color = new HsbColor(0, 0, 0.2);
compareNumbers(color.gray, 0.80002); compareNumbers(color.gray, 0.19998);
}); });
test('Get red from HSB Color', function() { test('Get red from HSB Color', function() {
@ -97,28 +97,29 @@ test('Get hue from RGB Color', function() {
test('Gray Color', function() { test('Gray Color', function() {
var color = new Color(1); var color = new Color(1);
compareNumbers(color.gray, 1); compareNumbers(color.gray, 1, 'color.gray');
compareNumbers(color.red, 0); compareNumbers(color.red, 1, 'color.red');
compareNumbers(color.blue, 0); compareNumbers(color.blue, 1, 'color.blue');
color.red = 0.5; color.red = 0.5;
compareNumbers(color.gray, 0.85055); console.log(color + '');
compareNumbers(color.gray, 0.85045, 'color.gray');
color.green = 0.2; color.green = 0.2;
compareNumbers(color.red, 0.5); compareNumbers(color.red, 0.5, 'color.red');
compareNumbers(color.green, 0.2); compareNumbers(color.green, 0.2, 'color.green');
compareNumbers(color.blue, 0); compareNumbers(color.blue, 1, 'color.blue');
compareNumbers(color.gray, 0.73315); compareNumbers(color.gray, 0.38085, 'color.gray');
}); });
test('Converting Colors', function() { test('Converting Colors', function() {
var rgbColor = new Color(1, 0.5, 0.2); var rgbColor = new Color(1, 0.5, 0.2);
compareNumbers(rgbColor.gray, 0.3848); compareNumbers(rgbColor.gray, 0.6152);
var grayColor = new Color(0.2); var grayColor = new Color(0.2);
compareColors(grayColor.convert('rgb'), new Color(0.8, 0.8, 0.8)); compareColors(grayColor.convert('rgb'), new Color(0.2, 0.2, 0.2));
compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.8 }); compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.2 });
compareColors(new Color(1, 0, 0).convert('hsb'), { hue: 0, saturation: 1, brightness: 1 }); compareColors(new Color(1, 0, 0).convert('hsb'), { hue: 0, saturation: 1, brightness: 1 });
}); });