dominicegginton / hex-color-opacity Goto Github PK
View Code? Open in Web Editor NEWLightweight module for adding opacity to hexadecimal colors
License: MIT License
Lightweight module for adding opacity to hexadecimal colors
License: MIT License
Hi, the Typescript definitions are missing from this project.
I added them manually in my project with
declare module "hex-color-opacity" {
export default function opacity(hex: string, opacity: number): string
}
But it would be cool if it was provided from the package itself.
All you would need to do is add a .d.ts
file in your package publication with something like this inside:
export default function opacity(hex: string, opacity: number): string
Hi, I've found a bug that when opacity value is really small (like 1% - 4%) function will generate wrong hex.
Expected: "#FF700003"
Received: "#FF70003"
32 | it('returns proper values', () => {
33 | expect(opacity('#FF7000', 0.0)).toBe('#FF700000')
> 34 | expect(opacity('#FF7000', 0.01)).toBe('#FF700003')
To fix I changed those lines:
if (opacity === 0) color += '00'
else if (opacity !== 1) color += Math.round(opacity * 255).toString(16)
return `#${color}`
into this:
const hexOpacity = Math.round(percentage * 255)
.toString(16)
.padStart(2, '0')
.toUpperCase()
return `#${color}${hexOpacity}`
This adds zeros at the beginning if generated opacity doesn't contain 2 characters. This also works for 0.0 value so no if
needed.
Add the ability to update a hex color code that already has an opacity value. Currently an error is thrown saying the hex code is invalid. The module should accept hexcodes with opacity applied, calulate and update the opacity value.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.