Skip to content

Commit

Permalink
chore(lint): remove magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Aug 18, 2022
1 parent 1fb1fb9 commit 81744be
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,18 @@ func hexToColor(hex string) (c color.RGBA) {
return c
}

const (
fullFormat = 7 // #RRGGBB
shortFormat = 4 // #RGB
)

switch len(hex) {
case 7: // #RRGGBB
c.R = hexToByte(hex[1])<<4 + hexToByte(hex[2])
c.G = hexToByte(hex[3])<<4 + hexToByte(hex[4])
c.B = hexToByte(hex[5])<<4 + hexToByte(hex[6])
case 4: // #RGB
case fullFormat:
const offset = 4
c.R = hexToByte(hex[1])<<offset + hexToByte(hex[2])
c.G = hexToByte(hex[3])<<offset + hexToByte(hex[4])
c.B = hexToByte(hex[5])<<offset + hexToByte(hex[6])
case shortFormat:
const offset = 0x11
c.R = hexToByte(hex[1]) * offset
c.G = hexToByte(hex[2]) * offset
Expand All @@ -213,13 +219,14 @@ func hexToColor(hex string) (c color.RGBA) {
}

func hexToByte(b byte) byte {
const offset = 10
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
return b - 'a' + offset
case b >= 'A' && b <= 'F':
return b - 'A' + 10
return b - 'A' + offset
}
// Invalid, but just return 0.
return 0
Expand Down

0 comments on commit 81744be

Please sign in to comment.