Skip to content

Commit

Permalink
Add color#hsv()
Browse files Browse the repository at this point in the history
  • Loading branch information
netsgnut committed Oct 2, 2018
1 parent 8397604 commit 9792c05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions autoload/pgmnt/color.vim
Expand Up @@ -115,6 +115,26 @@ function! pgmnt#color#hsl(h, s, l) abort
endfunction


function! pgmnt#color#hsv(h, s, v) abort
let h = s:constrain(a:h * 1.0, 0.0, 360.0)
let s = s:constrain(a:s * 1.0, 0.0, 1.0)
let v = s:constrain(a:v * 1.0, 0.0, 1.0)

let l = (2.0 - s) * v / 2.0
if l != 0.0
if l == 1.0
let s = 0.0
elseif l < 0.5
let s = a:s * a:v / (l * 2.0)
else
let s = a:s * a:v / (2.0 - l * 2.0)
endif
endif

return pgmnt#color#hsl(h, s, l)
endfunction


function! pgmnt#color#adjust_color(hex, options) abort
let hsl_comps = s:rgb_comps_to_hsl_comps(
\ s:hex_to_rgb_comps(a:hex)
Expand Down
28 changes: 28 additions & 0 deletions test/pgmnt/color.vim
Expand Up @@ -38,6 +38,34 @@ function! s:suite.test_hsl()
endfunction


function! s:suite.test_hsv()
call s:assert.equals(
\ pgmnt#color#hsv(0, 0, 0),
\ '#000000'
\ )
call s:assert.equals(
\ pgmnt#color#hsv(0, 0, 0.5),
\ '#7f7f7f'
\ )
call s:assert.equals(
\ pgmnt#color#hsv(0, 0, 1.0),
\ '#ffffff'
\ )
call s:assert.equals(
\ pgmnt#color#hsv(-10, -20, -30),
\ '#000000'
\ )
call s:assert.equals(
\ pgmnt#color#hsv(240, 1.0, 0.5),
\ '#00007f'
\ )
call s:assert.equals(
\ pgmnt#color#hsv(160, 0.25, 0.75),
\ '#8fbfaf'
\ )
endfunction


function! s:suite.test_darken()
call s:assert.equals(
\ pgmnt#color#darken('#daf5a3', 0.2),
Expand Down

0 comments on commit 9792c05

Please sign in to comment.