Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,14 @@ test("tetrad", function() {
var combination = tinycolor("red").tetrad();
equal(colorsToHexString(combination), "ff0000,80ff00,00ffff,7f00ff", "Correct Combination");
});


test("NumberHex", function () {
equal( tinycolor(0x0).toString(), "#000000")

equal( tinycolor("rgb(18, 0, 0)").toString(), tinycolor(0x120000).toString())
equal( tinycolor("rgb(0, 52, 0)").toString(), tinycolor(0x3400).toString())
equal( tinycolor("rgb(0, 0, 86)").toString(), tinycolor(0x56).toString())

equal( tinycolor("rgb(0, 0, 86)").toNumber(), 0x134)
})
25 changes: 24 additions & 1 deletion tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ function tinycolor (color, opts) {
color = (color) ? color : '';
opts = opts || { };


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you remove the whitespace changes in this file?


// If input is already a tinycolor, return itself
if (color instanceof tinycolor) {
return color;
}

// If we are called as a function, call using new instead
if (!(this instanceof tinycolor)) {
return new tinycolor(color, opts);
}


var rgb = inputToRGB(color);
this._originalInput = color,
this._r = rgb.r,
Expand Down Expand Up @@ -164,6 +168,9 @@ tinycolor.prototype = {

return "progid:DXImageTransform.Microsoft.gradient("+gradientType+"startColorstr="+hex8String+",endColorstr="+secondHex8String+")";
},
toNumber: function() {
return (mathRound(this._r)<<16) + (mathRound(this._g)<<8) + (mathRound(this._b))
},
toString: function(format) {
var formatSet = !!format;
format = format || this._format;
Expand Down Expand Up @@ -304,7 +311,12 @@ function inputToRGB(color) {
var ok = false;
var format = false;

if (typeof color == "string") {
if (typeof color == "number") {
color = numberInputToObject(color)
// color.toString(16);
// color = '#' + ('000000'+color).substring(color.length);
}
else if (typeof color == "string") {
color = stringInputToObject(color);
}

Expand Down Expand Up @@ -1064,6 +1076,17 @@ var matchers = (function() {
};
})();


// `numberInputToObject`
// Take in a number
function numberInputToObject(color) {
return {
r: color >> 16,
g: (color&0xff00) >> 8,
b: color&0xff
}
}

// `stringInputToObject`
// Permissive string parsing. Take in a number of formats, and output an object
// based on detected format. Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}`
Expand Down