Skip to content

Commit

Permalink
F26: update color similarity calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
stkrizh committed Jul 19, 2020
1 parent 7c57682 commit 389c5d3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
6 changes: 4 additions & 2 deletions colorific/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ async def get_images_by_color(
color: Color,
image_count: int = 36,
offset: int = 0,
percentage_weight: float = 0.5,
l_weight: float = 0.1,
percentage_weight: float = 0.1,
) -> List[Image]:
"""
Return images from DB ordered by color similarity.
Expand All @@ -26,7 +27,7 @@ async def get_images_by_color(
image.url_thumb,
MIN(
SQRT(
(c."L" / 100 - :L)^2 +
:l_weight * (c."L" / 100 - :L)^2 +
((c.a + 128) / 256 - :a)^2 +
((c.b + 128) / 256 - :b)^2 +
:percentage_weight * (c.percentage - 1)^2
Expand All @@ -47,6 +48,7 @@ async def get_images_by_color(
"L": color.L / 100,
"a": (color.a + 128) / 256,
"b": (color.b + 128) / 256,
"l_weight": 0.1,
"percentage_weight": percentage_weight,
"image_count": image_count,
"offset": offset,
Expand Down
13 changes: 3 additions & 10 deletions tests/test_image_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,15 @@ async def test_image_list(client):


@pytest.mark.parametrize(
"color, expected_order",
[
("ff0000", [5, 1]),
("ffffff", [3, 4, 5, 1]),
("000000", [2, 1, 5]),
("008800", [4, 5]),
],
"color, expected_id", [("ff0000", 5), ("ffffff", 3), ("000000", 2), ("008800", 4)]
)
async def test_image_ordering(client, color, expected_order):
async def test_image_ordering(client, color, expected_id):
response = await client.get(f"/images?color={color}")
assert response.status == 200
response_json = await response.json()
assert len(response_json) == 5
ids = [image["id"] for image in response_json]
for expected_id, actual_id in zip(expected_order, ids):
assert expected_id == actual_id
assert ids[0] == expected_id


@pytest.mark.parametrize(
Expand Down

0 comments on commit 389c5d3

Please sign in to comment.