Skip to content
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

able to accept number & export number #44

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions jquery.color.js
Expand Up @@ -245,6 +245,15 @@ color.fn = jQuery.extend( color.prototype, {
return this; return this;
} }


//binary color without alpha
if ( type === "number" ) {
source = red;
red = Math.floor( source / 0x10000 );
green = Math.floor( ( source - ( red * 0x10000 ) ) / 0x100 );
blue = source - ( red * 0x10000 ) - ( green * 0x100 );
return this.parse( [ red , green , blue ] );
}

if ( type === "object" ) { if ( type === "object" ) {
if ( red instanceof color ) { if ( red instanceof color ) {
each( spaces, function( spaceName, space ) { each( spaces, function( spaceName, space ) {
Expand Down Expand Up @@ -414,6 +423,10 @@ color.fn = jQuery.extend( color.prototype, {
return v.length === 1 ? "0" + v : v; return v.length === 1 ? "0" + v : v;
}).join(""); }).join("");
}, },
toNumber: function() {
var r = this._rgba[0], g = this._rgba[1], b = this._rgba[2];
return r * 0x10000 + g * 0x100 + b;
},
toString: function() { toString: function() {
return this._rgba[ 3 ] === 0 ? "transparent" : this.toRgbaString(); return this._rgba[ 3 ] === 0 ? "transparent" : this.toRgbaString();
} }
Expand Down
18 changes: 18 additions & 0 deletions test/unit/color.js
Expand Up @@ -605,4 +605,22 @@ test( "jQuery.Color.hook() - Create new hooks for color properties", 2, function
ok( jQuery.fx.step.testy, "fx.step testy hook created" ); ok( jQuery.fx.step.testy, "fx.step testy hook created" );
delete jQuery.cssHooks.testy; delete jQuery.cssHooks.testy;
delete jQuery.fx.step.testy; delete jQuery.fx.step.testy;
});

test( "jQuery.Color( 0xff0102 )", function() {
expect( 4 );
testParts( jQuery.Color( 0xff0102 ), {
expect: 4,
red: 255,
green: 1,
blue: 2,
alpha: 1
});
});

test( ".toNumber()", function() {
var almostBlack = jQuery.Color( "black" ).red( 2 ).blue( 16 );
expect( 1 );
equal( almostBlack.toNumber(), 0x020010, "to number");

}); });