From 60c8c6ce0256f712a290045befa1a5ea12a13524 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Wed, 9 Mar 2011 02:25:50 +0100 Subject: [PATCH] Add more color tests. --- test/tests/Color.js | 92 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/test/tests/Color.js b/test/tests/Color.js index aecf073c..88ece4c8 100644 --- a/test/tests/Color.js +++ b/test/tests/Color.js @@ -1,4 +1,4 @@ -module('RGB Color'); +module('Color'); test('Set named color', function() { var doc = new Document(); @@ -42,6 +42,54 @@ test('Set color to array', function() { equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)'); }); +test('Creating colors', function() { + + compareRGBColors(new RGBColor('#ff0000'), new RGBColor(1, 0, 0), + 'RGBColor from hex code'); + + compareRGBColors(new RGBColor({ red: 1, green: 0, blue: 1}), + new RGBColor(1, 0, 1), 'RGBColor from rgb object literal'); + + compareRGBColors(new RGBColor({ gray: 0.2 }), + new RGBColor(0.8, 0.8, 0.8), 'RGBColor from gray object literal'); + + compareRGBColors(new RGBColor({ hue: 0, saturation: 1, brightness: 1}), + new RGBColor(1, 0, 0), 'RGBColor from hsb object literal'); + + compareRGBColors(new RGBColor([1, 0, 0]), new RGBColor(1, 0, 0), + 'RGBColor from array'); + + compareHSBColors(new HSBColor('#000000'), new HSBColor(0, 0, 0), + 'HSBColor from hex code'); + + compareHSBColors(new HSBColor({ red: 1, green: 0, blue: 0}), + new HSBColor(0, 1, 1), 'HSBColor from rgb object literal'); + + compareHSBColors(new HSBColor({ gray: 0.8 }), + new HSBColor(0, 0, 0.2), 'RGBColor from gray object literal'); + + compareHSBColors(new HSBColor([1, 0, 0]), new HSBColor(1, 0, 0), + 'HSBColor from array'); + + 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'); +}); + test('Get gray from RGBColor', function() { var color = new RGBColor(1, 0.5, 0.2); compareNumbers(color.gray, 0.38458251953125); @@ -50,6 +98,22 @@ test('Get gray from RGBColor', function() { compareNumbers(color.gray, 0.72137451171875); }); +test('Get gray from HSBColor', function() { + var color = new HSBColor(0, 0, 0.2); + compareNumbers(color.gray, 0.8); +}); + +test('Get red from HSBColor', function() { + var color = new HSBColor(0, 1, 1); + compareNumbers(color.red, 1); +}); + +test('Get hue from RGBColor', function() { + var color = new RGBColor(1, 0, 0); + compareNumbers(color.hue, 0); + compareNumbers(color.saturation, 1); +}); + test('Gray Color', function() { var color = new GrayColor(1); compareNumbers(color.gray, 1); @@ -63,16 +127,34 @@ test('Gray Color', function() { }); test('Converting Colors', function() { - var color = new RGBColor(1, 0.5, 0.2); - compareNumbers(new GrayColor(color).gray, 0.38299560546875); + var rgbColor = new RGBColor(1, 0.5, 0.2); + compareNumbers(new GrayColor(rgbColor).gray, 0.38299560546875); - var color = new GrayColor(0.2); - var rgbColor = new RGBColor(color); + var grayColor = new GrayColor(0.2); + var rgbColor = new RGBColor(grayColor); compareRGBColors(rgbColor, [ 0.8, 0.8, 0.8, 1]); + + var hsbColor = new HSBColor(grayColor); + compareHSBColors(hsbColor, [ 0, 0, 0.8, 1]); + + var rgbColor = new RGBColor(1, 0, 0); + compareHSBColors(new HSBColor(rgbColor), [0, 1, 1, 1]); }); test('Setting RGBColor#gray', function() { var color = new RGBColor(1, 0.5, 0.2); color.gray = 0.1; compareRGBColors(color, [ 0.9, 0.9, 0.9, 1]); +}); + +test('Setting HSBColor#red', function() { + var color = new HSBColor(180, 0, 0); + color.red = 1; + compareHSBColors(color, [0, 1, 1, 1]); +}); + +test('Setting HSBColor#gray', function() { + var color = new HSBColor(180, 0, 0); + color.gray = 0.5; + compareHSBColors(color, [0, 0, 0.5, 1]); }); \ No newline at end of file