mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
More work on Color and testing.
This commit is contained in:
parent
0aa6c66681
commit
8292112ecd
5 changed files with 46 additions and 39 deletions
|
@ -221,7 +221,7 @@ var Color = this.Color = Base.extend(new function() {
|
|||
|
||||
initialize: function(arg) {
|
||||
// We are storing color internally as an array of components
|
||||
var argType = arg && typeof arg,
|
||||
var argType = arg != null && typeof arg,
|
||||
type,
|
||||
components = argType === 'number'
|
||||
? arguments
|
||||
|
|
|
@ -60,32 +60,36 @@ function asyncTest(testName, expected) {
|
|||
}
|
||||
|
||||
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) {
|
||||
compareNumbers(point1.x, point2.x,
|
||||
(message || '') + ' x');
|
||||
compareNumbers(point1.y, point2.y,
|
||||
(message || '') + ' y');
|
||||
compareNumbers(point1.x, point2.x, (message || '') + ' x');
|
||||
compareNumbers(point1.y, point2.y, (message || '') + ' y');
|
||||
}
|
||||
|
||||
function compareRectangles(rect1, rect2, message) {
|
||||
compareNumbers(rect1.x, rect2.x,
|
||||
(message || '') + ' x');
|
||||
compareNumbers(rect1.y, rect2.y,
|
||||
(message || '') + ' y');
|
||||
compareNumbers(rect1.width, rect2.width,
|
||||
(message || '') + ' width');
|
||||
compareNumbers(rect1.height, rect2.height,
|
||||
(message || '') + ' height');
|
||||
compareNumbers(rect1.x, rect2.x, (message || '') + ' x');
|
||||
compareNumbers(rect1.y, rect2.y, (message || '') + ' y');
|
||||
compareNumbers(rect1.width, rect2.width, (message || '') + ' width');
|
||||
compareNumbers(rect1.height, rect2.height, (message || '') + ' height');
|
||||
}
|
||||
|
||||
function compareColors(color1, color2, message) {
|
||||
color1 = new Color(color1);
|
||||
color2 = new Color(color2);
|
||||
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) {
|
||||
|
@ -95,8 +99,9 @@ function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
|
|||
return gradientColor[key] !== gradientColor2[key];
|
||||
}, true, 'Strict compare GradientColor#' + key);
|
||||
}
|
||||
equals(gradientColor[key] && gradientColor[key].toString(), gradientColor2[key] && gradientColor2[key].toString(),
|
||||
'Compare GradientColor#' + key);
|
||||
equals(gradientColor[key] && gradientColor[key].toString(),
|
||||
gradientColor2[key] && gradientColor2[key].toString(),
|
||||
'Compare GradientColor#' + key);
|
||||
});
|
||||
equals(function() {
|
||||
return gradientColor.gradient.equals(gradientColor2.gradient);
|
||||
|
@ -125,7 +130,8 @@ function comparePathStyles(style, style2, checkIdentity) {
|
|||
}
|
||||
compareGradientColors(style[key], style2[key], checkIdentity);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
@ -141,7 +147,7 @@ function comparePathStyles(style, style2, checkIdentity) {
|
|||
});
|
||||
|
||||
if (style.dashArray) {
|
||||
equals(style.dashArray.toString(), style2.dashArray.toString(),
|
||||
compareArrays(style.dashArray, style2.dashArray,
|
||||
'Compare CharacterStyle#dashArray');
|
||||
}
|
||||
}
|
||||
|
@ -250,7 +256,7 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) {
|
|||
'Compare Item#position');
|
||||
|
||||
equals(function() {
|
||||
return Base.equals(item.data, item2.data)
|
||||
return Base.equals(item.data, item2.data);
|
||||
}, true);
|
||||
|
||||
if (item.matrix) {
|
||||
|
@ -319,7 +325,8 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) {
|
|||
}
|
||||
equals(item.size.toString(), item2.size.toString(),
|
||||
'Compare Raster#size');
|
||||
equals(item.toDataURL() == item2.toDataURL(), true, 'Compare Raster#toDataUrl()')
|
||||
equals(item.toDataURL() == item2.toDataURL(), true,
|
||||
'Compare Raster#toDataUrl()');
|
||||
}
|
||||
|
||||
// TextItem specific:
|
||||
|
@ -371,7 +378,7 @@ function compareProjects(project, project2) {
|
|||
|
||||
// Compare Project#layers:
|
||||
equals(function() {
|
||||
return project.layers.length == project2.layers.length
|
||||
return project.layers.length == project2.layers.length;
|
||||
}, true);
|
||||
for (var i = 0, l = project.layers.length; i < l; i++) {
|
||||
compareItems(project.layers[i], project2.layers[i], false, false, true);
|
||||
|
|
|
@ -59,7 +59,7 @@ test('Creating colors', function() {
|
|||
new Color(1, 0, 1), 'Color from rgb object literal');
|
||||
|
||||
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}),
|
||||
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() {
|
||||
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);
|
||||
compareNumbers(color.gray, 0.72137451171875);
|
||||
compareNumbers(color.gray, 0.72175);
|
||||
});
|
||||
|
||||
test('Get gray from HSB Color', function() {
|
||||
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() {
|
||||
|
@ -115,7 +115,7 @@ test('Gray Color', function() {
|
|||
|
||||
test('Converting Colors', function() {
|
||||
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);
|
||||
compareColors(grayColor.convert('rgb'), new Color(0.8, 0.8, 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() {
|
||||
var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
|
||||
color.red = 1;
|
||||
compareColors(color, new Color(0, 1, 1));
|
||||
compareColors(color, new Color(1, 0, 0));
|
||||
});
|
||||
|
||||
test('Setting Color#gray', function() {
|
||||
|
|
|
@ -641,9 +641,9 @@ test('Item#blendMode in a transformed Group', function() {
|
|||
});
|
||||
|
||||
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:');
|
||||
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:');
|
||||
|
||||
raster.remove();
|
||||
|
@ -653,8 +653,8 @@ test('Item#blendMode in a transformed Group', function() {
|
|||
group.position = [50, 50];
|
||||
|
||||
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:');
|
||||
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:');
|
||||
});
|
||||
|
|
|
@ -78,12 +78,12 @@ asyncTest('Create a raster from a dom id', function(callback) {
|
|||
asyncTest('Raster#getPixel / setPixel', function(callback) {
|
||||
var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII=');
|
||||
raster.onLoad = function() {
|
||||
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0));
|
||||
compareColors(raster.getPixel(1, 0), new Color(0, 1, 0));
|
||||
compareColors(raster.getPixel(0, 1), new Color(0, 0, 1));
|
||||
compareColors(raster.getPixel(1, 1), new Color(1, 1, 1));
|
||||
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1));
|
||||
compareColors(raster.getPixel(1, 0), new Color(0, 1, 0, 1));
|
||||
compareColors(raster.getPixel(0, 1), new Color(0, 0, 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);
|
||||
compareColors(raster.getPixel([0, 0]), color);
|
||||
callback();
|
||||
|
@ -130,7 +130,7 @@ test('Raster#getAverageColor(path)', function() {
|
|||
});
|
||||
var raster = paper.project.activeLayer.rasterize();
|
||||
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() {
|
||||
|
@ -152,5 +152,5 @@ test('Raster#getAverageColor(path) with compound path', function() {
|
|||
var raster = paper.project.activeLayer.rasterize();
|
||||
path.scale(0.9);
|
||||
path2.scale(1.1);
|
||||
compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0));
|
||||
compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0, 1));
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue