More work on Color and testing.

This commit is contained in:
Jürg Lehni 2013-04-08 07:07:41 -07:00
parent 0aa6c66681
commit 8292112ecd
5 changed files with 46 additions and 39 deletions

View file

@ -221,7 +221,7 @@ var Color = this.Color = Base.extend(new function() {
initialize: function(arg) { initialize: function(arg) {
// We are storing color internally as an array of components // We are storing color internally as an array of components
var argType = arg && typeof arg, var argType = arg != null && typeof arg,
type, type,
components = argType === 'number' components = argType === 'number'
? arguments ? arguments

View file

@ -60,32 +60,36 @@ function asyncTest(testName, expected) {
} }
function compareNumbers(number1, number2, message) { function compareNumbers(number1, number2, message) {
equals(Format.number(number1, 5), Format.number(number2, 5), message); equals(Format.number(number1), Format.number(number2), message);
}
function compareArrays(array1, array2, message) {
function format(array) {
return Base.each(array, function(value, index) {
this[index] = Format.number(value);
}, []).toString();
}
equals(format(array1), format(array2), message);
} }
function comparePoints(point1, point2, message) { function comparePoints(point1, point2, message) {
compareNumbers(point1.x, point2.x, compareNumbers(point1.x, point2.x, (message || '') + ' x');
(message || '') + ' x'); compareNumbers(point1.y, point2.y, (message || '') + ' y');
compareNumbers(point1.y, point2.y,
(message || '') + ' y');
} }
function compareRectangles(rect1, rect2, message) { function compareRectangles(rect1, rect2, message) {
compareNumbers(rect1.x, rect2.x, compareNumbers(rect1.x, rect2.x, (message || '') + ' x');
(message || '') + ' x'); compareNumbers(rect1.y, rect2.y, (message || '') + ' y');
compareNumbers(rect1.y, rect2.y, compareNumbers(rect1.width, rect2.width, (message || '') + ' width');
(message || '') + ' y'); compareNumbers(rect1.height, rect2.height, (message || '') + ' height');
compareNumbers(rect1.width, rect2.width,
(message || '') + ' width');
compareNumbers(rect1.height, rect2.height,
(message || '') + ' height');
} }
function compareColors(color1, color2, message) { function compareColors(color1, color2, message) {
color1 = new Color(color1); color1 = new Color(color1);
color2 = new Color(color2); color2 = new Color(color2);
equals(color1.type, color2.type, (message || '') + ' type'); equals(color1.type, color2.type, (message || '') + ' type');
equals(color1.components.toString(), color2.components.toString(), (message || '') + ' components'); compareArrays(color1.components, color2.components,
(message || '') + ' components');
} }
function compareGradientColors(gradientColor, gradientColor2, checkIdentity) { function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
@ -95,8 +99,9 @@ function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
return gradientColor[key] !== gradientColor2[key]; return gradientColor[key] !== gradientColor2[key];
}, true, 'Strict compare GradientColor#' + key); }, true, 'Strict compare GradientColor#' + key);
} }
equals(gradientColor[key] && gradientColor[key].toString(), gradientColor2[key] && gradientColor2[key].toString(), equals(gradientColor[key] && gradientColor[key].toString(),
'Compare GradientColor#' + key); gradientColor2[key] && gradientColor2[key].toString(),
'Compare GradientColor#' + key);
}); });
equals(function() { equals(function() {
return gradientColor.gradient.equals(gradientColor2.gradient); return gradientColor.gradient.equals(gradientColor2.gradient);
@ -125,7 +130,8 @@ function comparePathStyles(style, style2, checkIdentity) {
} }
compareGradientColors(style[key], style2[key], checkIdentity); compareGradientColors(style[key], style2[key], checkIdentity);
} else { } else {
equals(style[key] && style[key].toString(), style2[key] && style2[key].toString(), equals(style[key] && style[key].toString(),
style2[key] && style2[key].toString(),
'Compare PathStyle#' + key); 'Compare PathStyle#' + key);
} }
} }
@ -141,7 +147,7 @@ function comparePathStyles(style, style2, checkIdentity) {
}); });
if (style.dashArray) { if (style.dashArray) {
equals(style.dashArray.toString(), style2.dashArray.toString(), compareArrays(style.dashArray, style2.dashArray,
'Compare CharacterStyle#dashArray'); 'Compare CharacterStyle#dashArray');
} }
} }
@ -250,7 +256,7 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) {
'Compare Item#position'); 'Compare Item#position');
equals(function() { equals(function() {
return Base.equals(item.data, item2.data) return Base.equals(item.data, item2.data);
}, true); }, true);
if (item.matrix) { if (item.matrix) {
@ -319,7 +325,8 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) {
} }
equals(item.size.toString(), item2.size.toString(), equals(item.size.toString(), item2.size.toString(),
'Compare Raster#size'); 'Compare Raster#size');
equals(item.toDataURL() == item2.toDataURL(), true, 'Compare Raster#toDataUrl()') equals(item.toDataURL() == item2.toDataURL(), true,
'Compare Raster#toDataUrl()');
} }
// TextItem specific: // TextItem specific:
@ -371,7 +378,7 @@ function compareProjects(project, project2) {
// Compare Project#layers: // Compare Project#layers:
equals(function() { equals(function() {
return project.layers.length == project2.layers.length return project.layers.length == project2.layers.length;
}, true); }, true);
for (var i = 0, l = project.layers.length; i < l; i++) { for (var i = 0, l = project.layers.length; i < l; i++) {
compareItems(project.layers[i], project2.layers[i], false, false, true); compareItems(project.layers[i], project2.layers[i], false, false, true);

View file

@ -59,7 +59,7 @@ test('Creating colors', function() {
new Color(1, 0, 1), 'Color from rgb object literal'); new Color(1, 0, 1), 'Color from rgb object literal');
compareColors(new Color({ gray: 0.2 }), compareColors(new Color({ gray: 0.2 }),
new Color(0.8, 0.8, 0.8).convert('gray'), 'Color from gray object literal'); new Color(0.2), 'Color from gray object literal');
compareColors(new Color({ hue: 0, saturation: 1, brightness: 1}), compareColors(new Color({ hue: 0, saturation: 1, brightness: 1}),
new Color(1, 0, 0).convert('hsb'), 'Color from hsb object literal'); new Color(1, 0, 0).convert('hsb'), 'Color from hsb object literal');
@ -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.38458251953125); compareNumbers(color.gray, 0.3848);
var color = new Color(0.5, 0.2, 0.1); var color = new Color(0.5, 0.2, 0.1);
compareNumbers(color.gray, 0.72137451171875); compareNumbers(color.gray, 0.72175);
}); });
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.8); compareNumbers(color.gray, 0.80002);
}); });
test('Get red from HSB Color', function() { test('Get red from HSB Color', function() {
@ -115,7 +115,7 @@ test('Gray Color', function() {
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.38299560546875); compareNumbers(rgbColor.gray, 0.3848);
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.8, 0.8, 0.8));
compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.8 }); compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.8 });
@ -131,7 +131,7 @@ test('Setting Color#gray', function() {
test('Setting Color#red', function() { test('Setting Color#red', function() {
var color = new Color({ hue: 180, saturation: 0, brightness: 0 }); var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
color.red = 1; color.red = 1;
compareColors(color, new Color(0, 1, 1)); compareColors(color, new Color(1, 0, 0));
}); });
test('Setting Color#gray', function() { test('Setting Color#gray', function() {

View file

@ -641,9 +641,9 @@ test('Item#blendMode in a transformed Group', function() {
}); });
var raster = layer.rasterize(); var raster = layer.rasterize();
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0), compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1),
'Top left pixel should be red:'); 'Top left pixel should be red:');
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0), compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1),
'Middle center pixel should be yellow:'); 'Middle center pixel should be yellow:');
raster.remove(); raster.remove();
@ -653,8 +653,8 @@ test('Item#blendMode in a transformed Group', function() {
group.position = [50, 50]; group.position = [50, 50];
var raster = layer.rasterize(); var raster = layer.rasterize();
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0), compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1),
'Top left pixel should be red:'); 'Top left pixel should be red:');
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0), compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1),
'Middle center pixel should be yellow:'); 'Middle center pixel should be yellow:');
}); });

View file

@ -78,12 +78,12 @@ asyncTest('Create a raster from a dom id', function(callback) {
asyncTest('Raster#getPixel / setPixel', function(callback) { asyncTest('Raster#getPixel / setPixel', function(callback) {
var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII='); var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII=');
raster.onLoad = function() { raster.onLoad = function() {
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0)); compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1));
compareColors(raster.getPixel(1, 0), new Color(0, 1, 0)); compareColors(raster.getPixel(1, 0), new Color(0, 1, 0, 1));
compareColors(raster.getPixel(0, 1), new Color(0, 0, 1)); compareColors(raster.getPixel(0, 1), new Color(0, 0, 1, 1));
compareColors(raster.getPixel(1, 1), new Color(1, 1, 1)); compareColors(raster.getPixel(1, 1), new Color(1, 1, 1, 1));
var color = new Color(1, 1, 0, 0.5); var color = new Color(1, 1, 0, 0.5, 1);
raster.setPixel([0, 0], color); raster.setPixel([0, 0], color);
compareColors(raster.getPixel([0, 0]), color); compareColors(raster.getPixel([0, 0]), color);
callback(); callback();
@ -130,7 +130,7 @@ test('Raster#getAverageColor(path)', function() {
}); });
var raster = paper.project.activeLayer.rasterize(); var raster = paper.project.activeLayer.rasterize();
path.scale(0.9); path.scale(0.9);
compareColors(raster.getAverageColor(path), new Color(1, 0, 0)); compareColors(raster.getAverageColor(path), new Color(1, 0, 0, 1));
}); });
test('Raster#getAverageColor(path) with compound path', function() { test('Raster#getAverageColor(path) with compound path', function() {
@ -152,5 +152,5 @@ test('Raster#getAverageColor(path) with compound path', function() {
var raster = paper.project.activeLayer.rasterize(); var raster = paper.project.activeLayer.rasterize();
path.scale(0.9); path.scale(0.9);
path2.scale(1.1); path2.scale(1.1);
compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0)); compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0, 1));
}); });