njsmith / colorspacious

A powerful, accurate, and easy-to-use Python library for doing colorspace conversions
MIT License
169 stars 16 forks source link

Error while using cvd_space #11

Open nicoguaro opened 6 years ago

nicoguaro commented 6 years ago

I am trying to convert the hsv colormap simulating colorblindness as described here. But I obtain the following error

ValueError: RGBA values should be within 0-1 range

Due to values outside of [0, 1].

Following an example

from __future__ import division, print_function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from colorspacious import cspace_convert

azimuths = np.arange(0, 361, 1)
zeniths = np.arange(40, 70, 1)
values = azimuths * np.ones((30, 361))
data = plt.cm.hsv(np.linspace(0, 1, 64))
fig = plt.figure()
cvd_space = {"name": "sRGB1+CVD",
     "cvd_type": "deuteranomaly",
     "severity": 50}
data2 = cspace_convert(data[:, :3], cvd_space, "sRGB1")
cmap = LinearSegmentedColormap.from_list('my_colormap', data2)
ax = plt.subplot(111, projection='polar')
ax.pcolormesh(azimuths*np.pi/180.0, zeniths, values, cmap=cmap)
ax.set_xticks([])
ax.set_yticks([])
njsmith commented 6 years ago

Yeah, the CVD model does that. If you're not doing anything weird, then the out of range values are probably barely out if range, like 1.000003 or -0.0007. In this case the simplest solution is to clip the values to fall in the 0-1 range, like the tutorial you linked to does using 'np.clip'. (The previous section of the tutorial, just above where you linked to, has a bit of discussion about this.)

On Jan 2, 2018 12:54 PM, "Nicolás Guarín-Zapata" notifications@github.com wrote:

I am trying to convert the hsv colormap simulating colorblindness as described here https://colorspacious.readthedocs.io/en/latest/tutorial.html#simulating-colorblindness. But I obtain the following error

ValueError: RGBA values should be within 0-1 range

Due to values outside of [0, 1].

Following an example

from future import division, print_function import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap from colorspacious import cspace_convert

azimuths = np.arange(0, 361, 1) zeniths = np.arange(40, 70, 1) values = azimuths np.ones((30, 361)) data = plt.cm.hsv(np.linspace(0, 1, 64)) fig = plt.figure() cvd_space = {"name": "sRGB1+CVD", "cvd_type": "deuteranomaly", "severity": 50} data2 = cspace_convert(data[:, :3], cvd_space, "sRGB1") cmap = LinearSegmentedColormap.from_list('my_colormap', data2) ax = plt.subplot(111, projection='polar') ax.pcolormesh(azimuthsnp.pi/180.0, zeniths, values, cmap=cmap) ax.set_xticks([]) ax.set_yticks([])

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/njsmith/colorspacious/issues/11, or mute the thread https://github.com/notifications/unsubscribe-auth/AAlOaMEFB0ITgLDHAT2OjWrUZ0Go6YMLks5tGpdpgaJpZM4RRENj .

nicoguaro commented 6 years ago

I thought about that, and it worked. The only problem is that I obtained values between -1.989 and 1.047 :-/

njsmith commented 6 years ago

Huh, yeah, that is weird. I just double-checked the paper, and it looks like we're implementing their formula correctly, and it really does just gives invalid values sometimes. And AFAICT they don't say anything about this or give any suggestions on how to handle it. I guess clipping is ... possibly not totally wrong? Not sure what to tell you.

nicoguaro commented 6 years ago

Thanks for double checking! You can see the plots I generated, if you are interested, here: https://nicoguaro.github.io/posts/cyclic_colormaps/