Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

able to accept number & export number #44

Closed
wants to merge 2 commits into from

4 participants

@Cactusbone

Hello!
I've added possibility to import/export numbers, i needed it to work with a webGL application where colors are numbers (0xff0102 for example).

@danheberden

Is there a reason youre doing manual parsing instead of (theInt).toString(16) and just parsing the hex like parseInt("0xff1234", 16)? And couldn't the end user do this perhaps? (instead of mudding up the API)

@Cactusbone

converting the number to string before parsing it using jQuery.color.parse would require padding (to ensure 6 digits).

I like just giving the color in any format to the library and getting a jQuery color and then getting the format i need out of it.

@gnarf
Owner

I need to think on this one a bit... I have seen multiple number based encoding methods for colors, and would hate to implement one of them now and later find out I need to support more than one...

@mikesherov
Collaborator

@gnarf37, I'm fine with this. If there are other numerical color formats, I haven't seen it.

@gnarf
Owner

The fact that this format doesn't include alpha really bugs me... My research into webGL shows that it uses an array of floats for RGBA for most of its work, and that this wouldn't be useful.

Can you show me the place where you needed it?

@gnarf
Owner

Too much silence here - closing for now. Can you please help me understand why we want this?

@gnarf gnarf closed this
@Cactusbone

sorry i forgot to update. it looks like this is the color format used by three.js http://mrdoob.github.com/three.js/docs/52/#Reference/Core/Color
i'm not sure there are other cases so maybe this should be handled in three.js instead.

@gnarf
Owner

yeah, i haven't seen this format for colors used anywhere else, and it doesn't include the alpha channel which scares me.

Sorry, but I don't think I'm gonna land it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 0 deletions.
  1. +13 −0 jquery.color.js
  2. +18 −0 test/unit/color.js
View
13 jquery.color.js
@@ -245,6 +245,15 @@ color.fn = jQuery.extend( color.prototype, {
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 ( red instanceof color ) {
each( spaces, function( spaceName, space ) {
@@ -414,6 +423,10 @@ color.fn = jQuery.extend( color.prototype, {
return v.length === 1 ? "0" + v : v;
}).join("");
},
+ toNumber: function() {
+ var r = this._rgba[0], g = this._rgba[1], b = this._rgba[2];
+ return r * 0x10000 + g * 0x100 + b;
+ },
toString: function() {
return this._rgba[ 3 ] === 0 ? "transparent" : this.toRgbaString();
}
View
18 test/unit/color.js
@@ -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" );
delete jQuery.cssHooks.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");
+
});
Something went wrong with that request. Please try again.