|
1 | 1 | from unittest import TestCase |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | from plotly.exceptions import PlotlyError |
4 | 6 | import plotly.colors as colors |
5 | 7 |
|
@@ -224,3 +226,65 @@ def test_n_colors(self): |
224 | 226 | ] |
225 | 227 |
|
226 | 228 | self.assertEqual(generated_colorscale, expected_colorscale) |
| 229 | + |
| 230 | + |
| 231 | +@pytest.mark.parametrize( |
| 232 | + "colorscale,low,high,colortype,expected", |
| 233 | + [ |
| 234 | + ("Viridis", 0.0, 1.0, "rgb", ["rgb(68, 1, 84)"]), |
| 235 | + ("Viridis_r", 0.0, 1.0, "rgb", ["rgb(253, 231, 37)"]), |
| 236 | + (["#000000", "#ffffff"], 0.0, 1.0, "tuple", [(0.0, 0.0, 0.0)]), |
| 237 | + ( |
| 238 | + [[0.0, "#ff0000"], [1.0, "#0000ff"]], |
| 239 | + 0.25, |
| 240 | + 0.75, |
| 241 | + "tuple", |
| 242 | + [(0.75, 0.0, 0.25)], |
| 243 | + ), |
| 244 | + (["#000000", "#ffffff"], 0.5, 0.5, "tuple", [(0.5, 0.5, 0.5)]), |
| 245 | + (["#000000", "#ffffff"], 1.0, 0.0, "rgb", ["rgb(255, 255, 255)"]), |
| 246 | + ], |
| 247 | +) |
| 248 | +def test_sample_colorscale_single_color(colorscale, low, high, colortype, expected): |
| 249 | + assert ( |
| 250 | + colors.sample_colorscale(colorscale, 1, low=low, high=high, colortype=colortype) |
| 251 | + == expected |
| 252 | + ) |
| 253 | + |
| 254 | + |
| 255 | +@pytest.mark.parametrize( |
| 256 | + "lowcolor,highcolor,colortype,expected", |
| 257 | + [ |
| 258 | + ((0, 0, 0), (1, 1, 1), "tuple", [(0.0, 0.0, 0.0)]), |
| 259 | + ("rgb(255,0,0)", "rgb(0,255,0)", "rgb", ["rgb(255.0, 0.0, 0.0)"]), |
| 260 | + ], |
| 261 | +) |
| 262 | +def test_n_colors_single_color(lowcolor, highcolor, colortype, expected): |
| 263 | + assert colors.n_colors(lowcolor, highcolor, 1, colortype=colortype) == expected |
| 264 | + |
| 265 | + |
| 266 | +@pytest.mark.parametrize( |
| 267 | + "count,expected", |
| 268 | + [ |
| 269 | + (0, []), |
| 270 | + (2, [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]), |
| 271 | + (3, [(0.0, 0.0, 0.0), (0.5, 0.5, 0.5), (1.0, 1.0, 1.0)]), |
| 272 | + ], |
| 273 | +) |
| 274 | +def test_sample_colorscale_empty_and_multiple_colors(count, expected): |
| 275 | + assert ( |
| 276 | + colors.sample_colorscale(["#000000", "#ffffff"], count, colortype="tuple") |
| 277 | + == expected |
| 278 | + ) |
| 279 | + |
| 280 | + |
| 281 | +@pytest.mark.parametrize( |
| 282 | + "count,expected", |
| 283 | + [ |
| 284 | + (0, []), |
| 285 | + (2, [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]), |
| 286 | + (3, [(0.0, 0.0, 0.0), (0.5, 0.5, 0.5), (1.0, 1.0, 1.0)]), |
| 287 | + ], |
| 288 | +) |
| 289 | +def test_n_colors_empty_and_multiple_colors(count, expected): |
| 290 | + assert colors.n_colors((0, 0, 0), (1, 1, 1), count) == expected |
0 commit comments