Skip to content

Commit

Permalink
Merge pull request #27 from mgreystone/bugfix-hex-to-rgba-css
Browse files Browse the repository at this point in the history
Fix `hexToRgbaCss` when alpha is 0
  • Loading branch information
rpearce authored May 23, 2022
2 parents 9e669e8 + cf3b0ba commit 7cd945b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.2] - 2022-05-23
* fixes bug with `hexToRgbaCss` when passed an alpha of 0

## [1.0.1] - 2020-08-29

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions __tests__/hexToRgbaCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ test('prefixed hex-3', () => {
})

test('alpha provided', () => {
expect(hexToRgbaCss('0099ff', 0)).toEqual('rgba(0,153,255,0)')
expect(hexToRgbaCss('0099ff80', 0.3)).toEqual('rgba(0,153,255,0.3)')
expect(hexToRgbaCss('0099ff', 0.2)).toEqual('rgba(0,153,255,0.2)')
expect(hexToRgbaCss('09f9', 0.7)).toEqual('rgba(0,153,255,0.7)')
expect(hexToRgbaCss('09f', 0.45)).toEqual('rgba(0,153,255,0.45)')
expect(hexToRgbaCss('0099ff', 1)).toEqual('rgba(0,153,255,1)')
})
2 changes: 1 addition & 1 deletion source/hexToRgbaCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface HexToRgbaCss {

const hexToRgbaCss: HexToRgbaCss = (str, alpha) => {
const [r, g, b, _a] = hexToRgba(str)
const a = alpha || _a
const a = alpha ?? _a
return rgbaToRgbaCss([r, g, b, a])
}

Expand Down

0 comments on commit 7cd945b

Please sign in to comment.