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

Add .hexa() method #237

Merged
merged 1 commit into from
Jan 11, 2022
Merged
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
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