mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
174 lines
No EOL
4.5 KiB
JavaScript
174 lines
No EOL
4.5 KiB
JavaScript
/*
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
* http://paperjs.org/
|
|
*
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
*
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
*
|
|
* All rights reserved.
|
|
*/
|
|
|
|
module('Color');
|
|
|
|
test('Set named color', function() {
|
|
var path = new Path();
|
|
path.fillColor = 'red';
|
|
compareColors(path.fillColor, new Color(1, 0, 0));
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
|
});
|
|
|
|
test('Set color to hex', function() {
|
|
var path = new Path();
|
|
path.fillColor = '#ff0000';
|
|
compareColors(path.fillColor, new Color(1, 0, 0));
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
|
|
|
var path = new Path();
|
|
path.fillColor = '#f00';
|
|
compareColors(path.fillColor, new Color(1, 0, 0));
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
|
});
|
|
|
|
test('Set color to object', function() {
|
|
var path = new Path();
|
|
path.fillColor = { red: 1, green: 0, blue: 1};
|
|
compareColors(path.fillColor, new Color(1, 0, 1));
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 255)');
|
|
|
|
var path = new Path();
|
|
path.fillColor = { gray: 0.2 };
|
|
compareColors(path.fillColor, new Color(0.2));
|
|
equals(path.fillColor.toCss(), 'rgb(204, 204, 204)');
|
|
});
|
|
|
|
test('Set color to array', function() {
|
|
var path = new Path();
|
|
path.fillColor = [1, 0, 0];
|
|
compareColors(path.fillColor, new Color(1, 0, 0));
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
|
});
|
|
|
|
test('Creating colors', function() {
|
|
|
|
compareColors(new Color('#ff0000'), new Color(1, 0, 0),
|
|
'Color from hex code');
|
|
|
|
compareColors(new Color({ red: 1, green: 0, blue: 1}),
|
|
new Color(1, 0, 1), 'Color from rgb object literal');
|
|
|
|
compareColors(new Color({ gray: 0.2 }),
|
|
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');
|
|
|
|
compareColors(new Color([1, 0, 0]), new Color(1, 0, 0),
|
|
'Rgb Color from array');
|
|
|
|
compareColors(new Color([1]), new Color(1),
|
|
'Gray Color from array');
|
|
});
|
|
|
|
test('Get gray from RGB Color', function() {
|
|
var color = new Color(1, 0.5, 0.2);
|
|
compareNumbers(color.gray, 0.3848);
|
|
|
|
var color = new Color(0.5, 0.2, 0.1);
|
|
compareNumbers(color.gray, 0.72175);
|
|
});
|
|
|
|
test('Get gray from HSB Color', function() {
|
|
var color = new HsbColor(0, 0, 0.2);
|
|
compareNumbers(color.gray, 0.80002);
|
|
});
|
|
|
|
test('Get red from HSB Color', function() {
|
|
var color = new HsbColor(0, 1, 1);
|
|
compareNumbers(color.red, 1);
|
|
});
|
|
|
|
test('Get hue from RGB Color', function() {
|
|
var color = new Color(1, 0, 0);
|
|
compareNumbers(color.hue, 0);
|
|
compareNumbers(color.saturation, 1);
|
|
});
|
|
|
|
test('Gray Color', function() {
|
|
var color = new Color(1);
|
|
compareNumbers(color.gray, 1);
|
|
compareNumbers(color.red, 0);
|
|
compareNumbers(color.blue, 0);
|
|
|
|
color.red = 0.5;
|
|
compareNumbers(color.gray, 0.85055);
|
|
|
|
color.green = 0.2;
|
|
|
|
compareNumbers(color.red, 0.5);
|
|
compareNumbers(color.green, 0.2);
|
|
compareNumbers(color.blue, 0);
|
|
|
|
compareNumbers(color.gray, 0.73315);
|
|
});
|
|
|
|
test('Converting Colors', function() {
|
|
var rgbColor = new Color(1, 0.5, 0.2);
|
|
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 });
|
|
compareColors(new Color(1, 0, 0).convert('hsb'), { hue: 0, saturation: 1, brightness: 1 });
|
|
});
|
|
|
|
test('Setting Color#gray', function() {
|
|
var color = new Color(1, 0.5, 0.2);
|
|
color.gray = 0.1;
|
|
compareColors(color, new Color(0.1));
|
|
});
|
|
|
|
test('Setting Color#red', function() {
|
|
var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
|
|
color.red = 1;
|
|
compareColors(color, new Color(1, 0, 0));
|
|
});
|
|
|
|
test('Setting Color#gray', function() {
|
|
var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
|
|
color.gray = 0.5;
|
|
compareColors(color, new Color(0.5));
|
|
});
|
|
|
|
test('Color.read(channels)', function() {
|
|
var color = Color.read([0, 0, 1]);
|
|
compareColors(color, new Color(0, 0, 1));
|
|
});
|
|
|
|
test('Cloning colors', function() {
|
|
var color = new Color(0, 0, 0);
|
|
equals(function() {
|
|
return color.clone() != color;
|
|
}, true);
|
|
|
|
equals(function() {
|
|
return new Color(color) != color;
|
|
}, true);
|
|
});
|
|
|
|
test('Color#convert', function() {
|
|
var color = new Color(0, 0, 0);
|
|
var converted = color.convert('rgb');
|
|
equals(function() {
|
|
return converted !== color;
|
|
}, true);
|
|
equals(function() {
|
|
return converted.equals(color);
|
|
}, true);
|
|
});
|
|
|
|
test('Saturation from black rgb', function() {
|
|
equals(function() {
|
|
return new Color(0, 0, 0).saturation === 0;
|
|
}, true);
|
|
}); |