2011-07-01 06:17:45 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-07-01 06:17:45 -04:00
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-07-01 06:17:45 -04:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-03-08 20:25:50 -05:00
|
|
|
module('Color');
|
2011-02-19 10:48:59 -05:00
|
|
|
|
|
|
|
test('Set named color', function() {
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = 'red';
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(1, 0, 0));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
2011-02-19 10:48:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Set color to hex', function() {
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = '#ff0000';
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(1, 0, 0));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
2011-02-19 10:48:59 -05:00
|
|
|
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = '#f00';
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(1, 0, 0));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
2011-02-19 10:48:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Set color to object', function() {
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = { red: 1, green: 0, blue: 1};
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(1, 0, 1));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 255)');
|
2011-02-19 10:48:59 -05:00
|
|
|
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = { gray: 0.2 };
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(0.8, 0.8, 0.8));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(204, 204, 204)');
|
2011-02-19 10:48:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Set color to array', function() {
|
|
|
|
var path = new Path();
|
|
|
|
path.fillColor = [1, 0, 0];
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(path.fillColor, new Color(1, 0, 0));
|
2012-12-01 15:31:22 -05:00
|
|
|
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
|
2011-02-19 10:48:59 -05:00
|
|
|
});
|
|
|
|
|
2011-03-08 20:25:50 -05:00
|
|
|
test('Creating colors', function() {
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(new Color('#ff0000'), new Color(1, 0, 0),
|
2011-11-10 13:16:34 -05:00
|
|
|
'RgbColor from hex code');
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(new Color({ red: 1, green: 0, blue: 1}),
|
|
|
|
new Color(1, 0, 1), 'RgbColor from rgb object literal');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(new Color({ gray: 0.2 }),
|
|
|
|
new Color(0.8, 0.8, 0.8), 'RgbColor from gray object literal');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(new Color({ hue: 0, saturation: 1, brightness: 1}),
|
|
|
|
new Color(1, 0, 0), 'RgbColor from hsb object literal');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
compareRgbColors(new Color([1, 0, 0]), new Color(1, 0, 0),
|
2011-11-10 13:16:34 -05:00
|
|
|
'RgbColor from array');
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(new HsbColor('#000000'), new HsbColor(0, 0, 0),
|
|
|
|
'HsbColor from hex code');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(new HsbColor({ red: 1, green: 0, blue: 0}),
|
|
|
|
new HsbColor(0, 1, 1), 'HsbColor from rgb object literal');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(new HsbColor({ gray: 0.8 }),
|
|
|
|
new HsbColor(0, 0, 0.2), 'RgbColor from gray object literal');
|
2011-03-08 20:25:50 -05:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(new HsbColor([1, 0, 0]), new HsbColor(1, 0, 0),
|
|
|
|
'HsbColor from array');
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-03-08 20:25:50 -05:00
|
|
|
compareGrayColors(new GrayColor('#000000'), new GrayColor(1),
|
|
|
|
'GrayColor from hex code');
|
|
|
|
|
|
|
|
compareGrayColors(new GrayColor('#ffffff'), new GrayColor(0),
|
|
|
|
'GrayColor from hex code');
|
|
|
|
|
|
|
|
compareGrayColors(new GrayColor({ red: 1, green: 1, blue: 1}),
|
|
|
|
new GrayColor(0), 'GrayColor from rgb object literal');
|
|
|
|
|
|
|
|
compareGrayColors(new GrayColor({ gray: 0.2 }),
|
|
|
|
new GrayColor(0.2), 'GrayColor from gray object literal');
|
|
|
|
|
|
|
|
compareGrayColors(new GrayColor({ hue: 0, saturation: 0, brightness: 0.8}),
|
|
|
|
new GrayColor(0.2), 'GrayColor from hsb object literal');
|
|
|
|
|
|
|
|
compareGrayColors(new GrayColor([1]), new GrayColor(1),
|
|
|
|
'GrayColor from array');
|
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Get gray from RgbColor', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(1, 0.5, 0.2);
|
2011-02-19 10:48:59 -05:00
|
|
|
compareNumbers(color.gray, 0.38458251953125);
|
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(0.5, 0.2, 0.1);
|
2011-02-19 10:48:59 -05:00
|
|
|
compareNumbers(color.gray, 0.72137451171875);
|
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Get gray from HsbColor', function() {
|
|
|
|
var color = new HsbColor(0, 0, 0.2);
|
2011-03-08 20:25:50 -05:00
|
|
|
compareNumbers(color.gray, 0.8);
|
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Get red from HsbColor', function() {
|
|
|
|
var color = new HsbColor(0, 1, 1);
|
2011-03-08 20:25:50 -05:00
|
|
|
compareNumbers(color.red, 1);
|
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Get hue from RgbColor', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(1, 0, 0);
|
2011-03-08 20:25:50 -05:00
|
|
|
compareNumbers(color.hue, 0);
|
2011-07-07 10:09:02 -04:00
|
|
|
compareNumbers(color.saturation, 1);
|
2011-03-08 20:25:50 -05:00
|
|
|
});
|
|
|
|
|
2011-02-19 10:48:59 -05:00
|
|
|
test('Gray Color', function() {
|
|
|
|
var color = new GrayColor(1);
|
|
|
|
compareNumbers(color.gray, 1);
|
2011-02-19 11:11:17 -05:00
|
|
|
compareNumbers(color.red, 0);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-02-19 10:48:59 -05:00
|
|
|
color.red = 0.5;
|
|
|
|
compareNumbers(color.gray, '0.84999');
|
|
|
|
|
|
|
|
color.green = 0.2;
|
|
|
|
compareNumbers(color.gray, '0.82051');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Converting Colors', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var rgbColor = new Color(1, 0.5, 0.2);
|
2011-03-08 20:25:50 -05:00
|
|
|
compareNumbers(new GrayColor(rgbColor).gray, 0.38299560546875);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-03-08 20:25:50 -05:00
|
|
|
var grayColor = new GrayColor(0.2);
|
2013-04-07 13:03:51 -04:00
|
|
|
var rgbColor = new Color(grayColor);
|
2011-11-10 13:16:34 -05:00
|
|
|
compareRgbColors(rgbColor, [ 0.8, 0.8, 0.8, 1]);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
var hsbColor = new HsbColor(grayColor);
|
|
|
|
compareHsbColors(hsbColor, [ 0, 0, 0.8, 1]);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2013-04-07 13:03:51 -04:00
|
|
|
var rgbColor = new Color(1, 0, 0);
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(new HsbColor(rgbColor), [0, 1, 1, 1]);
|
2011-02-24 07:00:46 -05:00
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Setting RgbColor#gray', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(1, 0.5, 0.2);
|
2011-02-24 07:00:46 -05:00
|
|
|
color.gray = 0.1;
|
2011-11-10 13:16:34 -05:00
|
|
|
compareRgbColors(color, [ 0.9, 0.9, 0.9, 1]);
|
2011-03-08 20:25:50 -05:00
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Setting HsbColor#red', function() {
|
|
|
|
var color = new HsbColor(180, 0, 0);
|
2011-03-08 20:25:50 -05:00
|
|
|
color.red = 1;
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(color, [0, 1, 1, 1]);
|
2011-03-08 20:25:50 -05:00
|
|
|
});
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
test('Setting HsbColor#gray', function() {
|
|
|
|
var color = new HsbColor(180, 0, 0);
|
2011-03-08 20:25:50 -05:00
|
|
|
color.gray = 0.5;
|
2011-11-10 13:16:34 -05:00
|
|
|
compareHsbColors(color, [0, 0, 0.5, 1]);
|
2011-05-15 08:11:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Color.read(channels)', function() {
|
|
|
|
var color = Color.read([0, 0, 1]);
|
2011-11-10 13:16:34 -05:00
|
|
|
compareRgbColors(color, [0, 0, 1, 1]);
|
2011-05-21 07:34:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Cloning colors', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(0, 0, 0);
|
2011-05-21 07:34:27 -04:00
|
|
|
equals(function() {
|
|
|
|
return color.clone() != color;
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
equals(function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
return new Color(color) != color;
|
2011-05-21 07:34:27 -04:00
|
|
|
}, true);
|
2011-05-21 11:25:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Color#convert', function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
var color = new Color(0, 0, 0);
|
2011-05-21 11:25:05 -04:00
|
|
|
var converted = color.convert('rgb');
|
|
|
|
equals(function() {
|
|
|
|
return converted !== color;
|
|
|
|
}, true);
|
|
|
|
equals(function() {
|
2011-05-21 15:02:06 -04:00
|
|
|
return converted.equals(color);
|
2011-05-21 11:25:05 -04:00
|
|
|
}, true);
|
2011-08-13 09:25:29 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Saturation from black rgb', function() {
|
|
|
|
equals(function() {
|
2013-04-07 13:03:51 -04:00
|
|
|
return new Color(0, 0, 0).saturation == 0;
|
2011-08-13 09:25:29 -04:00
|
|
|
}, true);
|
2011-02-19 10:48:59 -05:00
|
|
|
});
|