|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from branca.colormap import _parse_color_as_numerical_sequence |
4 |
| - |
5 |
| - |
6 |
| -@pytest.mark.parametrize("input_data, expected", [ |
7 |
| - ((0, 0, 0), (0.0, 0.0, 0.0, 1.0)), |
8 |
| - ((255, 255, 255), (1.0, 1.0, 1.0, 1.0)), |
9 |
| - ((255, 0, 0), (1.0, 0.0, 0.0, 1.0)), |
10 |
| - ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)), |
11 |
| - ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)), |
12 |
| - ((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)), |
13 |
| - ((0.0, 1.0, 0.0, 0.5), (0.0, 1.0, 0.0, 0.5)), |
14 |
| - ((0, 0, 0, 255.0), (0.0, 0.0, 0.0, 1.0)), |
15 |
| - ((0, 0, 255.0, 0.0), (0.0, 0.0, 1.0, 0.0)), |
16 |
| -]) |
| 3 | +from branca.colormap import ( |
| 4 | + _parse_color_as_numerical_sequence, |
| 5 | + _color_normalized_float_to_byte_int, |
| 6 | + _color_byte_to_normalized_float, |
| 7 | + _parse_hex, |
| 8 | + _is_hex, |
| 9 | + _parse_color, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "input_data, expected", |
| 15 | + [ |
| 16 | + ((255, 0, 0), (1.0, 0.0, 0.0, 1.0)), |
| 17 | + ((255, 0, 0, 127), (1.0, 0.0, 0.0, 0.4980392156862745)), |
| 18 | + ("#FF0000", (1.0, 0.0, 0.0, 1.0)), |
| 19 | + ("red", (1.0, 0.0, 0.0, 1.0)), |
| 20 | + ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)), |
| 21 | + ((0.25, 0.5, 0.75), (0.25, 0.5, 0.75, 1.0)), |
| 22 | + ((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)), |
| 23 | + ("#0000FF", (0.0, 0.0, 1.0, 1.0)), |
| 24 | + ("#00FF00", (0.0, 1.0, 0.0, 1.0)), |
| 25 | + ("#FFFFFF", (1.0, 1.0, 1.0, 1.0)), |
| 26 | + ("#000000", (0.0, 0.0, 0.0, 1.0)), |
| 27 | + ("#808080", (0.5019607843137255, 0.5019607843137255, 0.5019607843137255, 1.0)), |
| 28 | + ( |
| 29 | + "#1A2B3C", |
| 30 | + (0.10196078431372549, 0.16862745098039217, 0.23529411764705882, 1.0), |
| 31 | + ), |
| 32 | + ("green", (0.0, 0.5019607843137255, 0.0, 1.0)), |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_parse_color(input_data, expected): |
| 36 | + assert _parse_color(input_data) == expected |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "input_data, expected", |
| 41 | + [ |
| 42 | + ((0, 0, 0), (0.0, 0.0, 0.0, 1.0)), |
| 43 | + ((255, 255, 255), (1.0, 1.0, 1.0, 1.0)), |
| 44 | + ((255, 0, 0), (1.0, 0.0, 0.0, 1.0)), |
| 45 | + ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)), |
| 46 | + ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 1.0)), |
| 47 | + ((0.1, 0.2, 0.3, 0.4), (0.1, 0.2, 0.3, 0.4)), |
| 48 | + ((0.0, 1.0, 0.0, 0.5), (0.0, 1.0, 0.0, 0.5)), |
| 49 | + ((0, 0, 0, 255.0), (0.0, 0.0, 0.0, 1.0)), |
| 50 | + ((0, 0, 255.0, 0.0), (0.0, 0.0, 1.0, 0.0)), |
| 51 | + ], |
| 52 | +) |
17 | 53 | def test_parse_color_as_numerical_sequence(input_data, expected):
|
18 | 54 | assert _parse_color_as_numerical_sequence(input_data) == expected
|
19 | 55 |
|
20 |
| -@pytest.mark.parametrize("input_data, raises", [ |
21 |
| - ((256, 0, 0), ValueError), |
22 |
| - ((0, 0, -1), ValueError), |
23 |
| - ((0, 1, 2, 3, 4), ValueError), |
24 |
| - ((0.5, 0.5, 0.5, "string"), TypeError), |
25 |
| -]) |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "input_data, raises", |
| 59 | + [ |
| 60 | + ((256, 0, 0), ValueError), |
| 61 | + ((0, 0, -1), ValueError), |
| 62 | + ((0, 1, 2, 3, 4), ValueError), |
| 63 | + ((0.5, 0.5, 0.5, "string"), TypeError), |
| 64 | + ], |
| 65 | +) |
26 | 66 | def test_parse_color_as_numerical_sequence_invalid(input_data, raises):
|
27 | 67 | with pytest.raises(raises):
|
28 | 68 | _parse_color_as_numerical_sequence(input_data)
|
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + "input_data, expected", |
| 73 | + [ |
| 74 | + ("#123456", True), |
| 75 | + ("#abcdef", True), |
| 76 | + ("#ABCDEF", True), |
| 77 | + ("#1A2B3C", True), |
| 78 | + ("#123", False), |
| 79 | + ("123456", False), |
| 80 | + ("#1234567", False), |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_is_hex(input_data, expected): |
| 84 | + assert _is_hex(input_data) == expected |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "input_data, expected", |
| 89 | + [ |
| 90 | + ("#000000", (0.0, 0.0, 0.0, 1.0)), |
| 91 | + ("#FFFFFF", (1.0, 1.0, 1.0, 1.0)), |
| 92 | + ("#FF0000", (1.0, 0.0, 0.0, 1.0)), |
| 93 | + ("#00FF00", (0.0, 1.0, 0.0, 1.0)), |
| 94 | + ("#0000FF", (0.0, 0.0, 1.0, 1.0)), |
| 95 | + ("#808080", (0.5019607843137255, 0.5019607843137255, 0.5019607843137255, 1.0)), |
| 96 | + ], |
| 97 | +) |
| 98 | +def test_parse_hex(input_data, expected): |
| 99 | + assert _parse_hex(input_data) == expected |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + "input_data, expected", |
| 104 | + [ |
| 105 | + (0, 0.0), |
| 106 | + (255, 1.0), |
| 107 | + (128, 0.5019607843137255), |
| 108 | + (64, 0.25098039215686274), |
| 109 | + (192, 0.7529411764705882), |
| 110 | + ], |
| 111 | +) |
| 112 | +def test_color_byte_to_normalized_float(input_data, expected): |
| 113 | + assert _color_byte_to_normalized_float(input_data) == expected |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.parametrize( |
| 117 | + "input_data, expected", |
| 118 | + [ |
| 119 | + (0.0, 0), |
| 120 | + (0.5, 127), |
| 121 | + (1.0, 255), |
| 122 | + (0.9999, 255), |
| 123 | + (0.1, 25), |
| 124 | + (0.75, 191), |
| 125 | + ], |
| 126 | +) |
| 127 | +def test_color_normalized_float_to_byte_int(input_data, expected): |
| 128 | + assert _color_normalized_float_to_byte_int(input_data) == expected |
0 commit comments