Skip to content

added handling of numbers for Pixi.js hex format with tests #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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(1, 1, 1)").toNumber(), (1<<16)+(1<<8)+1)
})
21 changes: 19 additions & 2 deletions tinycolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function tinycolor (color, opts) {

color = (color) ? color : '';
opts = opts || { };

// If input is already a tinycolor, return itself
if (color instanceof tinycolor) {
return color;
Expand Down Expand Up @@ -164,6 +164,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 +307,10 @@ function inputToRGB(color) {
var ok = false;
var format = false;

if (typeof color == "string") {
if (typeof color == "number") {
color = numberInputToObject(color);
}
else if (typeof color == "string") {
color = stringInputToObject(color);
}

Expand Down Expand Up @@ -1064,6 +1070,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