2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-02-17 21:31:38 +03:00
|
|
|
from PIL import GimpGradientFile, ImagePalette
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_linear_pos_le_middle():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.25
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.linear(middle, pos)
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.25
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_linear_pos_le_small_middle():
|
|
|
|
# Arrange
|
|
|
|
middle = 1e-11
|
|
|
|
pos = 1e-12
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.linear(middle, pos)
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.0
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_linear_pos_gt_middle():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.75
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.linear(middle, pos)
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.75
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_linear_pos_gt_small_middle():
|
|
|
|
# Arrange
|
|
|
|
middle = 1 - 1e-11
|
|
|
|
pos = 1 - 1e-12
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.linear(middle, pos)
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 1.0
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_curved():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.75
|
2014-08-26 13:21:08 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.curved(middle, pos)
|
2014-08-26 13:21:08 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.75
|
2014-08-26 13:21:08 +04:00
|
|
|
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sine():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.75
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.sine(middle, pos)
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.8535533905932737
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sphere_increasing():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.75
|
2014-08-26 12:45:28 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.sphere_increasing(middle, pos)
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert round(abs(ret - 0.9682458365518543), 7) == 0
|
2014-08-26 13:04:04 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sphere_decreasing():
|
|
|
|
# Arrange
|
|
|
|
middle = 0.5
|
|
|
|
pos = 0.75
|
2014-08-26 13:04:04 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ret = GimpGradientFile.sphere_decreasing(middle, pos)
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert ret == 0.3385621722338523
|
2014-08-26 12:45:28 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_load_via_imagepalette():
|
|
|
|
# Arrange
|
|
|
|
test_file = "Tests/images/gimp_gradient.ggr"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
palette = ImagePalette.load(test_file)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
# load returns raw palette information
|
|
|
|
assert len(palette[0]) == 1024
|
|
|
|
assert palette[1] == "RGBA"
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_1_3_via_imagepalette():
|
|
|
|
# Arrange
|
|
|
|
# GIMP 1.3 gradient files contain a name field
|
|
|
|
test_file = "Tests/images/gimp_gradient_with_name.ggr"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
palette = ImagePalette.load(test_file)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
# load returns raw palette information
|
|
|
|
assert len(palette[0]) == 1024
|
|
|
|
assert palette[1] == "RGBA"
|