Skip to content

Commit

Permalink
Merge branch 'blvz-feature/hex-modes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Aisch committed May 23, 2015
2 parents ad354a6 + 50a147c commit 617794d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -47,7 +47,7 @@ You can use it in node.js, too!

### Build instructions

To compile the coffee-script source files you have to run (might have to ``npm install`` first)
To compile the coffee-script source files you have to run (might have to ``npm install` first)

grunt

Expand Down
7 changes: 2 additions & 5 deletions src/color.coffee
Expand Up @@ -123,8 +123,8 @@ class Color
rgba: ->
@_rgb

hex: ->
rgb2hex @_rgb
hex: (mode='rgb') ->
rgb2hex @_rgb, mode

toString: ->
@name()
Expand Down Expand Up @@ -322,6 +322,3 @@ class Color

desaturate: (amount=20) ->
@saturate -amount



12 changes: 9 additions & 3 deletions src/conversions/rgb2hex.coffee
@@ -1,6 +1,12 @@

rgb2hex = () ->
[r,g,b] = unpack arguments
rgb2hex = (channels, mode='rgb') ->
[r,g,b,a] = channels
u = r << 16 | g << 8 | b
str = "000000" + u.toString(16) #.toUpperCase()
"#" + str.substr(str.length - 6)
str = str.substr(str.length - 6)
hxa = '0' + Math.round(a * 255).toString(16)
hxa = hxa.substr(hxa.length - 2)
"#" + switch mode.toLowerCase()
when 'rgba' then str + hxa
when 'argb' then hxa + str
else str
6 changes: 6 additions & 0 deletions test/apha-test.coffee → test/alpha-test.coffee
Expand Up @@ -67,4 +67,10 @@ vows
topic: chroma.css 'hsla(0,100%,50%,0.25)'
'cssoutput': -> (topic) -> assert.equal topic.css(), 'rgba(255,0,0,0.25)'

'hex output':
topic: chroma.gl 1, 0, 0, 0
'hex': (topic) -> assert.equal topic.hex(), '#ff0000'
'rgba': (topic) -> assert.equal topic.hex('rgba'), '#ff000000'
'argb': (topic) -> assert.equal topic.hex('argb'), '#00ff0000'

.export(module)
2 changes: 2 additions & 0 deletions test/colors-test.coffee
Expand Up @@ -18,6 +18,8 @@ vows
topic: chroma '#f00'
'name': (topic) -> assert.equal topic.name(), 'red'
'hex': (topic) -> assert.equal topic.hex(), '#ff0000'
'hex rgba': (topic) -> assert.equal topic.hex('rgba'), '#ff0000ff'
'hex argb': (topic) -> assert.equal topic.hex('argb'), '#ffff0000'
'rgb': (topic) -> assert.deepEqual topic.rgb(), [255,0,0]

'hex color, no #':
Expand Down

0 comments on commit 617794d

Please sign in to comment.