bgrins / TinyColor

Fast, small color manipulation and conversion for JavaScript
https://bgrins.github.io/TinyColor/
MIT License
5.05k stars 438 forks source link

Support AARRGGBB #192

Open justbittin opened 6 years ago

justbittin commented 6 years ago

First, this library is amazing.

I would really like to see a way to switch between the current alpha 8 hex (rrggbbaa) to android and other 8 hex format (aarrggbb).

Maybe a constructor option?

Is this a possibility?

CrandellWS commented 5 years ago

screenshot example of usage...

screenshot from 2018-11-04 06-08-16

Let me know if it suits your needs or if you have any suggestion /feedback... Thanks

justbittin commented 5 years ago

That's perfect! Thank you.

justbittin commented 5 years ago

I think the only addition needed now would be a

argb(a,r,g,b) input. Again, that's how android takes it.

It just makes it easier when jumping in and out of my backend (java) to keep it consistent.

In Android:

ARGB representation(#AARRGGBB)

AA - Alpha component [0..255] of the color RR - Red component [0..255] of the color GG - Green component [0..255] of the color BB - Blue component [0..255] of the color

So the current constructor for rgba doesn't work without me doing some math to set the alpha (0-1) where droid expects 0-255.

Just another suggestion, and would be great to have :-)

Thank you so much.

CrandellWS commented 5 years ago

the function toHex8ArgbString() I believe outputs as you desire

this is based on https://developer.android.com/guide/topics/resources/more-resources#Color

assuming your using https://github.com/CrandellWS/TinyColor/blob/toHex8Argb/tinycolor.js

justbittin commented 5 years ago

Yes sir, switched to yours immediately.

I'm concerned with the initial input.

I use tinycolor in my hyrid app, and the colors coming from java are all

(Aa, rr, gg, bb) and, they are in range 0-255 even for alpha. Not current 0-1 range.

Am I making sense? :-)

CrandellWS commented 5 years ago

I guess after re reading comment @justbittin

Using my modifications you can add an input option to flag it as #AARRGGBB

//{"hex8Argb": true}
var ArgbColor = '#aaFFFFFF';
var myColor = tinycolor(ArgbColor, {"hex8Argb": true});
myColor.toHex8ArgbString()//"#aaffffff"

screenshot from 2018-11-04 13-01-51

justbittin commented 5 years ago

We are on different pages, the hex part is perfect. I'm commenting on inputing a rgba - which in android, is argb. And, even in your to8Argbu - yo are outputting a decimal between 0 and 1 for alpha. Android uses 0-255 for alpha as well.

CrandellWS commented 5 years ago

arbg(0,0,0,0)

As far as I know ARGB is done in hex format as shown in the link I gave previously to Android docs.

Can you link to Android documentation on this?

justbittin commented 5 years ago

https://developer.android.com/reference/android/graphics/Color

justbittin commented 5 years ago

https://developer.android.com/reference/android/graphics/Color.html#argb(int,%20int,%20int,%20int)

CrandellWS commented 5 years ago

ok give me a bit I will make it happen

CrandellWS commented 5 years ago

might be tomorrow though I am about to sleep

CrandellWS commented 5 years ago

1/2 way there.... gn

screenshot from 2018-11-04 13-45-32

Android support is what I am after as well... thanks for pointing this out

CrandellWS commented 5 years ago

ok I think I got it please test it out and let me know if there is any issues

JS file: https://github.com/CrandellWS/TinyColor/blob/toHex8Argb/tinycolor.js

Changes: https://github.com/CrandellWS/TinyColor/commit/c8187a7a6ad1b3fd3f292c558c164eaf1bc53f18

screenshot from 2018-11-05 10-48-44

@justbittin good day man

CrandellWS commented 5 years ago

ok I rushed that and the alpha value is not being read correctly

CrandellWS commented 5 years ago

https://github.com/CrandellWS/TinyColor/commit/1a6d1ae

https://codepen.io/anon/pen/MzwKBZ

it is functioning but be warned that the alpha is not 100% accurate... screenshot from 2018-11-05 14-35-09

justbittin commented 5 years ago

Ok, I was just about to dl. I'll wait for you to update me :-)

One thing to also remember, when putting output from the 8Argb don't use 0-1 for alpha. Android doesn't use the 0.0 to 1 for alpha, but 0-255 like he links above.

Thanks for all this.

mattfelten commented 3 years ago

Will this be an official thing? Looks stale and would love a way to do this.