Giter Site home page Giter Site logo

hex-color-opacity's Introduction

Software developer at Arup, climber and open-source advocate from the Midlands UK.

hex-color-opacity's People

Contributors

dependabot[bot] avatar dominicegginton avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

hex-color-opacity's Issues

Add Typescript support

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

Fails to generate colors for small values

Hi, I've found a bug that when opacity value is really small (like 1% - 4%) function will generate wrong hex.

Failing test example:

    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')

Fix

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.

Support updating hex opacity

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.