Skip to content

Commit

Permalink
Fix set_sat
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jul 10, 2019
1 parent b7d665c commit 6c16fff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pilgram/css/blending/nonseparable.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ def set_sat(c, s):
cmid = r + g + b - cmax - cmin
new_cmid = ((cmid - cmin) * s) / (cmax - cmin)

cmid_r = (cmax > cmin) * (cmid == r) * new_cmid
cmid_g = (cmax > cmin) * (cmid == g) * new_cmid
cmid_b = (cmax > cmin) * (cmid == b) * new_cmid
# NOTE: use cmax if cmax == cmid
cmid_r = (cmax > cmin) * (cmax > cmid) * (cmid == r) * new_cmid
cmid_g = (cmax > cmin) * (cmax > cmid) * (cmid == g) * new_cmid
cmid_b = (cmax > cmin) * (cmax > cmid) * (cmid == b) * new_cmid

cmax_r = (cmax > cmin) * (cmax == r) * s
cmax_g = (cmax > cmin) * (cmax == g) * s
Expand Down
51 changes: 48 additions & 3 deletions pilgram/css/blending/tests/test_nonseparable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pilgram.css.blending.nonseparable import _min3, _max3
from pilgram.css.blending.nonseparable import _clip_color
from pilgram.css.blending.nonseparable import lum, lum_im, set_lum
from pilgram.css.blending.nonseparable import sat
from pilgram.css.blending.nonseparable import sat, set_sat


def test_min3():
Expand Down Expand Up @@ -110,5 +110,50 @@ def test_sat():
assert list(im_sat.getdata()) == [120]


def test_set_sat():
pass # TODO
def test_set_sat_cmax_gt_cmin():
im1 = util.fill((1, 1), [0, 128, 255])
im2 = util.fill((1, 1), [64, 96, 128]) # sat = 64
r1, g1, b1 = im1.split()
r2, g2, b2 = im2.split()
bands = ImageMath.eval(
'set_sat((r1, g1, b1), sat((r2, g2, b2)))',
set_sat=set_sat, sat=sat,
r1=r1, g1=g1, b1=b1,
r2=r2, g2=g2, b2=b2)

expected = [
[0],
[pytest.approx(32.12549019607843, abs=1)],
[64],
]
assert [list(band.im.getdata()) for band in bands] == expected


def test_set_sat_cmax_eq_cmid_gt_cmin():
im1 = util.fill((1, 1), [0, 128, 128])
im2 = util.fill((1, 1), [64, 96, 128]) # sat = 64
r1, g1, b1 = im1.split()
r2, g2, b2 = im2.split()
bands = ImageMath.eval(
'set_sat((r1, g1, b1), sat((r2, g2, b2)))',
set_sat=set_sat, sat=sat,
r1=r1, g1=g1, b1=b1,
r2=r2, g2=g2, b2=b2)

expected = [[0], [64], [64]]
assert [list(band.im.getdata()) for band in bands] == expected


def test_set_sat_cmax_eq_cmin():
im1 = util.fill((1, 1), [128, 128, 128])
im2 = util.fill((1, 1), [64, 96, 128]) # sat = 64
r1, g1, b1 = im1.split()
r2, g2, b2 = im2.split()
bands = ImageMath.eval(
'set_sat((r1, g1, b1), sat((r2, g2, b2)))',
set_sat=set_sat, sat=sat,
r1=r1, g1=g1, b1=b1,
r2=r2, g2=g2, b2=b2)

expected = [[0], [0], [0]]
assert [list(band.im.getdata()) for band in bands] == expected

0 comments on commit 6c16fff

Please sign in to comment.