JSLint rgbcolor.js

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2702 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Brett Zamir 2014-02-17 07:05:29 +00:00
parent 14397f4b19
commit 463207276a
1 changed files with 31 additions and 24 deletions

View File

@ -1,15 +1,15 @@
/*jslint vars: true*/
/** /**
* A class to parse color values * A class to parse color values
* @author Stoyan Stefanov <sstoo@gmail.com> * @author Stoyan Stefanov <sstoo@gmail.com>
* @link http://www.phpied.com/rgb-color-parser-in-javascript/ * @link http://www.phpied.com/rgb-color-parser-in-javascript/
* @license Use it if you like it * @license Use it if you like it
*/ */
function RGBColor(color_string) function RGBColor(color_string) { 'use strict';
{
this.ok = false; this.ok = false;
// strip any leading # // strip any leading #
if (color_string.charAt(0) == '#') { // remove # if any if (color_string.charAt(0) === '#') { // remove # if any
color_string = color_string.substr(1,6); color_string = color_string.substr(1,6);
} }
@ -163,9 +163,12 @@ function RGBColor(color_string)
yellow: 'ffff00', yellow: 'ffff00',
yellowgreen: '9acd32' yellowgreen: '9acd32'
}; };
for (var key in simple_colors) { var key;
if (color_string == key) { for (key in simple_colors) {
color_string = simple_colors[key]; if (simple_colors.hasOwnProperty(key)) {
if (color_string == key) {
color_string = simple_colors[key];
}
} }
} }
// emd of simple type-in colors // emd of simple type-in colors
@ -177,9 +180,9 @@ function RGBColor(color_string)
example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'], example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
process: function (bits){ process: function (bits){
return [ return [
parseInt(bits[1]), parseInt(bits[1], 10),
parseInt(bits[2]), parseInt(bits[2], 10),
parseInt(bits[3]) parseInt(bits[3], 10)
]; ];
} }
}, },
@ -207,13 +210,14 @@ function RGBColor(color_string)
} }
]; ];
var i;
// search through the definitions to find a match // search through the definitions to find a match
for (var i = 0; i < color_defs.length; i++) { for (i = 0; i < color_defs.length; i++) {
var re = color_defs[i].re; var re = color_defs[i].re;
var processor = color_defs[i].process; var processor = color_defs[i].process;
var bits = re.exec(color_string); var bits = re.exec(color_string);
if (bits) { if (bits) {
channels = processor(bits); var channels = processor(bits);
this.r = channels[0]; this.r = channels[0];
this.g = channels[1]; this.g = channels[1];
this.b = channels[2]; this.b = channels[2];
@ -230,36 +234,39 @@ function RGBColor(color_string)
// some getters // some getters
this.toRGB = function () { this.toRGB = function () {
return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')'; return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
} };
this.toHex = function () { this.toHex = function () {
var r = this.r.toString(16); var r = this.r.toString(16);
var g = this.g.toString(16); var g = this.g.toString(16);
var b = this.b.toString(16); var b = this.b.toString(16);
if (r.length == 1) r = '0' + r; if (r.length === 1) {r = '0' + r;}
if (g.length == 1) g = '0' + g; if (g.length === 1) {g = '0' + g;}
if (b.length == 1) b = '0' + b; if (b.length === 1) {b = '0' + b;}
return '#' + r + g + b; return '#' + r + g + b;
} };
// help // help
this.getHelpXML = function () { this.getHelpXML = function () {
var i, j;
var examples = new Array(); var examples = [];
// add regexps // add regexps
for (var i = 0; i < color_defs.length; i++) { for (i = 0; i < color_defs.length; i++) {
var example = color_defs[i].example; var example = color_defs[i].example;
for (var j = 0; j < example.length; j++) { for (j = 0; j < example.length; j++) {
examples[examples.length] = example[j]; examples[examples.length] = example[j];
} }
} }
// add type-in colors // add type-in colors
for (var sc in simple_colors) { var sc;
examples[examples.length] = sc; for (sc in simple_colors) {
if (simple_colors.hasOwnProperty(sc)) {
examples[examples.length] = sc;
}
} }
var xml = document.createElement('ul'); var xml = document.createElement('ul');
xml.setAttribute('id', 'rgbcolor-examples'); xml.setAttribute('id', 'rgbcolor-examples');
for (var i = 0; i < examples.length; i++) { for (i = 0; i < examples.length; i++) {
try { try {
var list_item = document.createElement('li'); var list_item = document.createElement('li');
var list_color = new RGBColor(examples[i]); var list_color = new RGBColor(examples[i]);
@ -282,6 +289,6 @@ function RGBColor(color_string)
} }
return xml; return xml;
} };
} }