diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 35bca1d7028f..24bdbf733b9f 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -254,6 +254,105 @@ def test_light_source_topo_surface(): ax.imshow(rgb) ax.set(xticks=[], yticks=[]) +def test_light_source_shading_default(): + """Array comparison test for the default "hsv" blend mode. Ensure the + default result doesn't change without warning.""" + y, x = np.mgrid[-1.2:1.2:8j, -1.2:1.2:8j] + z = 10 * np.cos(x**2 + y**2) + + cmap = plt.cm.copper + ls = mcolors.LightSource(315, 45) + rgb = ls.shade(z, cmap) + + r = np.array([[[ 0.87, 0.85, 0.9 , 0.9 , 0.82, 0.62, 0.34, 0. ], + [ 0.85, 0.94, 0.99, 1. , 1. , 0.96, 0.62, 0.17], + [ 0.9 , 0.99, 1. , 1. , 1. , 1. , 0.71, 0.33], + [ 0.9 , 1. , 1. , 1. , 1. , 0.98, 0.51, 0.29], + [ 0.82, 1. , 1. , 1. , 1. , 0.64, 0.25, 0.13], + [ 0.62, 0.96, 1. , 0.98, 0.64, 0.22, 0.06, 0.03], + [ 0.34, 0.62, 0.71, 0.51, 0.25, 0.06, 0. , 0.01], + [ 0. , 0.17, 0.33, 0.29, 0.13, 0.03, 0.01, 0. ]], + + [[ 0.87, 0.79, 0.83, 0.8 , 0.66, 0.44, 0.23, 0. ], + [ 0.79, 0.88, 0.93, 0.92, 0.83, 0.66, 0.38, 0.1 ], + [ 0.83, 0.93, 0.99, 1. , 0.92, 0.75, 0.4 , 0.18], + [ 0.8 , 0.92, 1. , 0.99, 0.93, 0.75, 0.28, 0.14], + [ 0.66, 0.83, 0.92, 0.93, 0.87, 0.44, 0.12, 0.06], + [ 0.44, 0.66, 0.75, 0.75, 0.44, 0.12, 0.03, 0.01], + [ 0.23, 0.38, 0.4 , 0.28, 0.12, 0.03, 0. , 0. ], + [ 0. , 0.1 , 0.18, 0.14, 0.06, 0.01, 0. , 0. ]], + + [[ 0.87, 0.75, 0.78, 0.73, 0.55, 0.33, 0.16, 0. ], + [ 0.75, 0.85, 0.9 , 0.86, 0.71, 0.48, 0.23, 0.05], + [ 0.78, 0.9 , 0.98, 1. , 0.82, 0.51, 0.21, 0.08], + [ 0.73, 0.86, 1. , 0.97, 0.84, 0.47, 0.11, 0.05], + [ 0.55, 0.71, 0.82, 0.84, 0.71, 0.2 , 0.03, 0.01], + [ 0.33, 0.48, 0.51, 0.47, 0.2 , 0.02, 0. , 0. ], + [ 0.16, 0.23, 0.21, 0.11, 0.03, 0. , 0. , 0. ], + [ 0. , 0.05, 0.08, 0.05, 0.01, 0. , 0. , 0. ]], + + [[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ]]]) + # Result stored transposed and rounded for for more compact display... + assert_array_almost_equal(rgb, r.T, decimal=2) + + +def test_light_source_masked_shading(): + """Array comparison test for a surface with a masked portion. Ensures that + we don't wind up with "fringes" of odd colors around masked regions.""" + y, x = np.mgrid[-1.2:1.2:8j, -1.2:1.2:8j] + z = 10 * np.cos(x**2 + y**2) + + z = np.ma.masked_greater(z, 9.9) + + cmap = plt.cm.copper + ls = mcolors.LightSource(315, 45) + rgb = ls.shade(z, cmap) + + r = np.array([[[ 1. , 0.95, 0.96, 0.94, 0.86, 0.67, 0.4 , 0.03], + [ 0.95, 0.99, 1. , 1. , 1. , 0.98, 0.67, 0.19], + [ 0.96, 1. , 1. , 1. , 1. , 1. , 0.78, 0.36], + [ 0.94, 1. , 1. , 0. , 0. , 1. , 0.55, 0.32], + [ 0.86, 1. , 1. , 0. , 0. , 1. , 0.27, 0.14], + [ 0.67, 0.98, 1. , 1. , 1. , 1. , 0.07, 0.03], + [ 0.4 , 0.67, 0.78, 0.55, 0.27, 0.07, 0. , 0.01], + [ 0.03, 0.19, 0.36, 0.32, 0.14, 0.03, 0.01, 0. ]], + + [[ 1. , 0.93, 0.93, 0.88, 0.72, 0.5 , 0.28, 0.03], + [ 0.93, 0.97, 0.99, 0.96, 0.87, 0.7 , 0.42, 0.11], + [ 0.93, 0.99, 0.74, 0.78, 0.78, 0.74, 0.45, 0.2 ], + [ 0.88, 0.96, 0.78, 0. , 0. , 0.78, 0.32, 0.16], + [ 0.72, 0.87, 0.78, 0. , 0. , 0.78, 0.14, 0.06], + [ 0.5 , 0.7 , 0.74, 0.78, 0.78, 0.74, 0.03, 0.01], + [ 0.28, 0.42, 0.45, 0.32, 0.14, 0.03, 0. , 0. ], + [ 0.03, 0.11, 0.2 , 0.16, 0.06, 0.01, 0. , 0. ]], + + [[ 1. , 0.91, 0.91, 0.84, 0.64, 0.39, 0.21, 0.03], + [ 0.91, 0.96, 0.98, 0.93, 0.77, 0.53, 0.27, 0.06], + [ 0.91, 0.98, 0.47, 0.5 , 0.5 , 0.47, 0.25, 0.1 ], + [ 0.84, 0.93, 0.5 , 0. , 0. , 0.5 , 0.13, 0.06], + [ 0.64, 0.77, 0.5 , 0. , 0. , 0.5 , 0.03, 0.01], + [ 0.39, 0.53, 0.47, 0.5 , 0.5 , 0.47, 0. , 0. ], + [ 0.21, 0.27, 0.25, 0.13, 0.03, 0. , 0. , 0. ], + [ 0.03, 0.06, 0.1 , 0.06, 0.01, 0. , 0. , 0. ]], + + [[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 0. , 0. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 0. , 0. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ], + [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ]]]) + # Result stored transposed and rounded for for more compact display... + assert_array_almost_equal(rgb, r.T, decimal=2) + def test_light_source_hillshading(): """Compare the current hillshading method against one that should be