Skip to content

Commit

Permalink
feat: rgba to hex support
Browse files Browse the repository at this point in the history
  • Loading branch information
headwindz authored and Qix- committed Jan 11, 2022
1 parent 0601ecf commit b071652
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ Color.prototype = {
return colorString.to.hex(this.rgb().round().color);
},

hexa(value) {
if (arguments.length > 0) {
return new Color(value);
}

const rgbArray = this.rgb().round().color;

let alphaHex = Math.round(this.valpha * 255).toString(16).toUpperCase();
if (alphaHex.length === 1) {
alphaHex = '0' + alphaHex;
}

return colorString.to.hex(rgbArray) + alphaHex;
},

rgbNumber() {
const rgb = this.rgb().color;
return ((rgb[0] & 0xFF) << 16) | ((rgb[1] & 0xFF) << 8) | (rgb[2] & 0xFF);
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ it('Translate with channel setters', () => {

it('CSS String getters', () => {
equal(Color('rgb(10, 30, 25)').hex(), '#0A1E19');
equal(Color('rgb(10, 30, 25, 1)').hexa(), '#0A1E19FF');
equal(Color('rgb(10, 30, 25, 0.4)').hexa(), '#0A1E1966');
equal(Color('rgb(10, 30, 25, 0)').hexa(), '#0A1E1900');
equal(Color('rgb(10, 30, 25, 0.01)').hexa(), '#0A1E1903');
equal(Color('rgb(10, 30, 25)').rgb().string(), 'rgb(10, 30, 25)');
equal(Color('rgb(10, 30, 25, 0.4)').rgb().string(), 'rgba(10, 30, 25, 0.4)');
equal(Color('rgb(10, 30, 25)').percentString(), 'rgb(4%, 12%, 10%)');
Expand Down

0 comments on commit b071652

Please sign in to comment.