2019-05-30 23:32:55 +05:30
|
|
|
import numpy as np
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
class Color():
|
2019-09-03 22:17:31 +05:30
|
|
|
"""Color representation in and conversion between different color-spaces."""
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2013-01-15 23:54:10 +05:30
|
|
|
__slots__ = [
|
|
|
|
'model',
|
2013-01-09 00:17:44 +05:30
|
|
|
'color',
|
2016-06-30 00:19:01 +05:30
|
|
|
'__dict__',
|
2013-01-09 00:17:44 +05:30
|
|
|
]
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-01-15 23:54:10 +05:30
|
|
|
def __init__(self,
|
|
|
|
model = 'RGB',
|
2015-06-15 21:06:58 +05:30
|
|
|
color = np.zeros(3,'d')):
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
|
|
|
Create a Color object.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
model : string
|
|
|
|
color model
|
|
|
|
color : numpy.ndarray
|
|
|
|
vector representing the color according to the selected model
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
2013-01-15 23:54:10 +05:30
|
|
|
self.__transforms__ = \
|
2017-08-29 01:09:35 +05:30
|
|
|
{'HSV': {'index': 0, 'next': self._HSV2HSL},
|
|
|
|
'HSL': {'index': 1, 'next': self._HSL2RGB, 'prev': self._HSL2HSV},
|
|
|
|
'RGB': {'index': 2, 'next': self._RGB2XYZ, 'prev': self._RGB2HSL},
|
|
|
|
'XYZ': {'index': 3, 'next': self._XYZ2CIELAB, 'prev': self._XYZ2RGB},
|
|
|
|
'CIELAB': {'index': 4, 'next': self._CIELAB2MSH, 'prev': self._CIELAB2XYZ},
|
|
|
|
'MSH': {'index': 5, 'prev': self._MSH2CIELAB},
|
2013-01-15 23:54:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
model = model.upper()
|
2016-07-02 16:58:32 +05:30
|
|
|
if model not in list(self.__transforms__.keys()): model = 'RGB'
|
2016-03-04 22:23:55 +05:30
|
|
|
if model == 'RGB' and max(color) > 1.0: # are we RGB255 ?
|
2013-01-15 23:54:10 +05:30
|
|
|
for i in range(3):
|
2016-03-04 22:23:55 +05:30
|
|
|
color[i] /= 255.0 # rescale to RGB
|
2013-01-15 23:54:10 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
if model == 'HSL': # are we HSL ?
|
|
|
|
if abs(color[0]) > 1.0: color[0] /= 360.0 # with angular hue?
|
|
|
|
while color[0] >= 1.0: color[0] -= 1.0 # rewind to proper range
|
|
|
|
while color[0] < 0.0: color[0] += 1.0 # rewind to proper range
|
2013-01-15 23:54:10 +05:30
|
|
|
|
|
|
|
self.model = model
|
2015-06-15 21:06:58 +05:30
|
|
|
self.color = np.array(color,'d')
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-01-15 23:54:10 +05:30
|
|
|
def __repr__(self):
|
2019-09-02 23:39:03 +05:30
|
|
|
"""Color model and values."""
|
2017-08-29 01:09:35 +05:30
|
|
|
return 'Model: %s Color: %s'%(self.model,str(self.color))
|
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-01-15 23:54:10 +05:30
|
|
|
def __str__(self):
|
2019-09-02 23:39:03 +05:30
|
|
|
"""Color model and values."""
|
2013-01-15 23:54:10 +05:30
|
|
|
return self.__repr__()
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-05-28 17:17:15 +05:30
|
|
|
def convertTo(self,toModel = 'RGB'):
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
|
|
|
Change the color model permanently.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
toModel : string
|
|
|
|
color model
|
|
|
|
|
|
|
|
"""
|
2013-01-15 23:54:10 +05:30
|
|
|
toModel = toModel.upper()
|
2017-08-29 01:09:35 +05:30
|
|
|
if toModel not in list(self.__transforms__.keys()): return
|
2013-01-15 23:54:10 +05:30
|
|
|
|
|
|
|
sourcePos = self.__transforms__[self.model]['index']
|
|
|
|
targetPos = self.__transforms__[toModel]['index']
|
|
|
|
|
|
|
|
while sourcePos < targetPos:
|
|
|
|
self.__transforms__[self.model]['next']()
|
|
|
|
sourcePos += 1
|
2013-01-04 19:27:36 +05:30
|
|
|
|
2013-01-15 23:54:10 +05:30
|
|
|
while sourcePos > targetPos:
|
|
|
|
self.__transforms__[self.model]['prev']()
|
|
|
|
sourcePos -= 1
|
|
|
|
return self
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-05-28 17:17:15 +05:30
|
|
|
def expressAs(self,asModel = 'RGB'):
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
|
|
|
Return the color in a different model.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
asModel : string
|
|
|
|
color model
|
|
|
|
|
|
|
|
"""
|
2017-08-29 01:09:35 +05:30
|
|
|
return self.__class__(self.model,self.color).convertTo(asModel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _HSV2HSL(self):
|
2017-08-29 01:21:47 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert H(ue) S(aturation) V(alue or brightness) to H(ue) S(aturation) L(uminance).
|
2017-08-29 01:21:47 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2019-09-03 11:34:42 +05:30
|
|
|
http://codeitdown.com/hsl-hsb-hsv-color
|
2017-08-29 01:09:35 +05:30
|
|
|
"""
|
|
|
|
if self.model != 'HSV': return
|
2017-08-29 01:23:21 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
converted = Color('HSL',np.array([
|
|
|
|
self.color[0],
|
|
|
|
1. if self.color[2] == 0.0 or (self.color[1] == 0.0 and self.color[2] == 1.0) \
|
|
|
|
else self.color[1]*self.color[2]/(1.-abs(self.color[2]*(2.-self.color[1])-1.)),
|
|
|
|
0.5*self.color[2]*(2.-self.color[1]),
|
|
|
|
]))
|
|
|
|
|
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
|
|
|
|
|
|
|
|
|
|
|
def _HSL2HSV(self):
|
2017-08-29 01:21:47 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert H(ue) S(aturation) L(uminance) to H(ue) S(aturation) V(alue or brightness).
|
2017-08-29 01:21:47 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2019-09-03 11:34:42 +05:30
|
|
|
http://codeitdown.com/hsl-hsb-hsv-color
|
2017-08-29 01:09:35 +05:30
|
|
|
"""
|
|
|
|
if self.model != 'HSL': return
|
|
|
|
|
|
|
|
h = self.color[0]
|
|
|
|
b = self.color[2]+0.5*(self.color[1]*(1.-abs(2*self.color[2]-1)))
|
|
|
|
s = 1.0 if b == 0.0 else 2.*(b-self.color[2])/b
|
|
|
|
|
|
|
|
converted = Color('HSV',np.array([h,s,b]))
|
|
|
|
|
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
def _HSL2RGB(self):
|
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert H(ue) S(aturation) L(uminance) to R(red) G(reen) B(lue).
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://en.wikipedia.org/wiki/HSL_and_HSV
|
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'HSL': return
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2013-02-03 01:47:02 +05:30
|
|
|
sextant = self.color[0]*6.0
|
2013-01-09 00:17:44 +05:30
|
|
|
c = (1.0 - abs(2.0 * self.color[2] - 1.0))*self.color[1]
|
|
|
|
x = c*(1.0 - abs(sextant%2 - 1.0))
|
|
|
|
m = self.color[2] - 0.5*c
|
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
converted = Color('RGB',np.array([
|
|
|
|
[c+m, x+m, m],
|
|
|
|
[x+m, c+m, m],
|
|
|
|
[m, c+m, x+m],
|
|
|
|
[m, x+m, c+m],
|
|
|
|
[x+m, m, c+m],
|
|
|
|
[c+m, m, x+m],
|
|
|
|
][int(sextant)],'d'))
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
2013-02-02 20:41:55 +05:30
|
|
|
self.color = converted.color
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _RGB2HSL(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert R(ed) G(reen) B(lue) to H(ue) S(aturation) L(uminance).
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2017-08-29 01:09:35 +05:30
|
|
|
from http://130.113.54.154/~monger/hsl-rgb.html
|
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'RGB': return
|
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
HSL = np.zeros(3,'d')
|
2013-01-09 00:17:44 +05:30
|
|
|
maxcolor = self.color.max()
|
|
|
|
mincolor = self.color.min()
|
|
|
|
HSL[2] = (maxcolor + mincolor)/2.0
|
|
|
|
if(mincolor == maxcolor):
|
|
|
|
HSL[0] = 0.0
|
|
|
|
HSL[1] = 0.0
|
|
|
|
else:
|
|
|
|
if (HSL[2]<0.5):
|
|
|
|
HSL[1] = (maxcolor - mincolor)/(maxcolor + mincolor)
|
2013-01-04 19:27:36 +05:30
|
|
|
else:
|
2013-02-02 20:41:55 +05:30
|
|
|
HSL[1] = (maxcolor - mincolor)/(2.0 - maxcolor - mincolor)
|
2013-01-09 00:17:44 +05:30
|
|
|
if (maxcolor == self.color[0]):
|
|
|
|
HSL[0] = 0.0 + (self.color[1] - self.color[2])/(maxcolor - mincolor)
|
|
|
|
elif (maxcolor == self.color[1]):
|
|
|
|
HSL[0] = 2.0 + (self.color[2] - self.color[0])/(maxcolor - mincolor)
|
|
|
|
elif (maxcolor == self.color[2]):
|
|
|
|
HSL[0] = 4.0 + (self.color[0] - self.color[1])/(maxcolor - mincolor)
|
2016-03-04 22:23:55 +05:30
|
|
|
HSL[0] = HSL[0]*60.0 # scaling to 360 might be dangerous for small values
|
2013-01-09 00:17:44 +05:30
|
|
|
if (HSL[0] < 0.0):
|
|
|
|
HSL[0] = HSL[0] + 360.0
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(2):
|
2017-08-29 01:09:35 +05:30
|
|
|
HSL[i+1] = min(HSL[i+1],1.0)
|
|
|
|
HSL[i+1] = max(HSL[i+1],0.0)
|
|
|
|
|
|
|
|
converted = Color('HSL', HSL)
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
def _RGB2XYZ(self):
|
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert R(ed) G(reen) B(lue) to CIE XYZ.
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://www.cs.rit.edu/~ncs/color/t_convert.html
|
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'RGB': return
|
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
XYZ = np.zeros(3,'d')
|
2015-06-15 21:06:58 +05:30
|
|
|
RGB_lin = np.zeros(3,'d')
|
|
|
|
convert = np.array([[0.412453,0.357580,0.180423],
|
|
|
|
[0.212671,0.715160,0.072169],
|
|
|
|
[0.019334,0.119193,0.950227]])
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(3):
|
2013-02-02 20:41:55 +05:30
|
|
|
if (self.color[i] > 0.04045): RGB_lin[i] = ((self.color[i]+0.0555)/1.0555)**2.4
|
|
|
|
else: RGB_lin[i] = self.color[i] /12.92
|
2015-06-15 21:06:58 +05:30
|
|
|
XYZ = np.dot(convert,RGB_lin)
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(3):
|
2013-01-09 00:17:44 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
XYZ[i] = max(XYZ[i],0.0)
|
|
|
|
|
|
|
|
converted = Color('XYZ', XYZ)
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _XYZ2RGB(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert CIE XYZ to R(ed) G(reen) B(lue).
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://www.cs.rit.edu/~ncs/color/t_convert.html
|
|
|
|
"""
|
2016-10-25 23:00:13 +05:30
|
|
|
if self.model != 'XYZ':
|
|
|
|
return
|
2013-01-09 00:17:44 +05:30
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
convert = np.array([[ 3.240479,-1.537150,-0.498535],
|
|
|
|
[-0.969256, 1.875992, 0.041556],
|
|
|
|
[ 0.055648,-0.204043, 1.057311]])
|
|
|
|
RGB_lin = np.dot(convert,self.color)
|
|
|
|
RGB = np.zeros(3,'d')
|
2013-02-02 20:41:55 +05:30
|
|
|
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(3):
|
2013-02-02 20:41:55 +05:30
|
|
|
if (RGB_lin[i] > 0.0031308): RGB[i] = ((RGB_lin[i])**(1.0/2.4))*1.0555-0.0555
|
|
|
|
else: RGB[i] = RGB_lin[i] *12.92
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(3):
|
2017-08-29 01:09:35 +05:30
|
|
|
RGB[i] = min(RGB[i],1.0)
|
|
|
|
RGB[i] = max(RGB[i],0.0)
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
maxVal = max(RGB) # clipping colors according to the display gamut
|
2013-02-02 20:41:55 +05:30
|
|
|
if (maxVal > 1.0): RGB /= maxVal
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
converted = Color('RGB', RGB)
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _CIELAB2XYZ(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert CIE Lab to CIE XYZ.
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://www.easyrgb.com/index.php?X=MATH&H=07#text7
|
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'CIELAB': return
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
ref_white = np.array([.95047, 1.00000, 1.08883],'d') # Observer = 2, Illuminant = D65
|
2017-08-29 01:09:35 +05:30
|
|
|
XYZ = np.zeros(3,'d')
|
2013-01-09 00:17:44 +05:30
|
|
|
|
2013-02-02 20:41:55 +05:30
|
|
|
XYZ[1] = (self.color[0] + 16.0 ) / 116.0
|
2013-02-03 01:47:02 +05:30
|
|
|
XYZ[0] = XYZ[1] + self.color[1]/ 500.0
|
|
|
|
XYZ[2] = XYZ[1] - self.color[2]/ 200.0
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(len(XYZ)):
|
2013-02-02 20:41:55 +05:30
|
|
|
if (XYZ[i] > 6./29. ): XYZ[i] = XYZ[i]**3.
|
2013-02-03 01:47:02 +05:30
|
|
|
else: XYZ[i] = 108./841. * (XYZ[i] - 4./29.)
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
converted = Color('XYZ', XYZ*ref_white)
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _XYZ2CIELAB(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert CIE XYZ to CIE Lab.
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2019-09-02 23:39:03 +05:30
|
|
|
All values are in the range [0,1]
|
2016-10-31 19:03:23 +05:30
|
|
|
from http://en.wikipedia.org/wiki/Lab_color_space,
|
2019-09-02 23:39:03 +05:30
|
|
|
http://www.cs.rit.edu/~ncs/color/t_convert.html
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'XYZ': return
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
ref_white = np.array([.95047, 1.00000, 1.08883],'d') # Observer = 2, Illuminant = D65
|
2013-01-09 00:17:44 +05:30
|
|
|
XYZ = self.color/ref_white
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-07-02 16:58:32 +05:30
|
|
|
for i in range(len(XYZ)):
|
2013-02-02 20:41:55 +05:30
|
|
|
if (XYZ[i] > 216./24389 ): XYZ[i] = XYZ[i]**(1.0/3.0)
|
2013-02-03 01:47:02 +05:30
|
|
|
else: XYZ[i] = (841./108. * XYZ[i]) + 16.0/116.0
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-15 21:06:58 +05:30
|
|
|
converted = Color('CIELAB', np.array([ 116.0 * XYZ[1] - 16.0,
|
|
|
|
500.0 * (XYZ[0] - XYZ[1]),
|
|
|
|
200.0 * (XYZ[1] - XYZ[2]) ]))
|
2013-01-09 00:17:44 +05:30
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _CIELAB2MSH(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert CIE Lab to Msh colorspace.
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
|
2017-08-29 01:09:35 +05:30
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'CIELAB': return
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
Msh = np.zeros(3,'d')
|
2020-01-02 22:32:13 +05:30
|
|
|
Msh[0] = np.sqrt(np.dot(self.color,self.color))
|
2013-02-03 01:47:02 +05:30
|
|
|
if (Msh[0] > 0.001):
|
2020-01-02 22:32:13 +05:30
|
|
|
Msh[1] = np.acos(self.color[0]/Msh[0])
|
2013-02-03 01:47:02 +05:30
|
|
|
if (self.color[1] != 0.0):
|
2020-01-02 22:32:13 +05:30
|
|
|
Msh[2] = np.atan2(self.color[2],self.color[1])
|
2013-01-09 00:17:44 +05:30
|
|
|
|
|
|
|
converted = Color('MSH', Msh)
|
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
def _MSH2CIELAB(self):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2019-09-02 23:39:03 +05:30
|
|
|
Convert Msh colorspace to CIE Lab.
|
2015-06-15 21:06:58 +05:30
|
|
|
|
2016-10-31 19:03:23 +05:30
|
|
|
with s,h in radians
|
2016-03-04 22:23:55 +05:30
|
|
|
from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
|
|
|
|
"""
|
2013-01-09 00:17:44 +05:30
|
|
|
if self.model != 'MSH': return
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
Lab = np.zeros(3,'d')
|
2020-01-02 22:32:13 +05:30
|
|
|
Lab[0] = self.color[0] * np.cos(self.color[1])
|
|
|
|
Lab[1] = self.color[0] * np.sin(self.color[1]) * np.cos(self.color[2])
|
|
|
|
Lab[2] = self.color[0] * np.sin(self.color[1]) * np.sin(self.color[2])
|
2013-01-09 00:17:44 +05:30
|
|
|
|
|
|
|
converted = Color('CIELAB', Lab)
|
|
|
|
self.model = converted.model
|
|
|
|
self.color = converted.color
|
2013-05-28 17:09:26 +05:30
|
|
|
|
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
class Colormap():
|
2019-09-03 11:34:42 +05:30
|
|
|
"""Perceptually uniform diverging or sequential colormap."""
|
2013-01-09 00:17:44 +05:30
|
|
|
|
2013-01-15 23:54:10 +05:30
|
|
|
__slots__ = [
|
2013-01-09 00:17:44 +05:30
|
|
|
'left',
|
|
|
|
'right',
|
2013-05-29 01:45:13 +05:30
|
|
|
'interpolate',
|
2017-08-29 01:09:35 +05:30
|
|
|
]
|
2013-05-28 19:34:25 +05:30
|
|
|
__predefined__ = {
|
2016-04-11 23:17:07 +05:30
|
|
|
'gray': {'left': Color('HSL',[0,1,1]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0,0,0.15]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'grey': {'left': Color('HSL',[0,1,1]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0,0,0.15]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'red': {'left': Color('HSL',[0,1,0.14]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0,0.35,0.91]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'green': {'left': Color('HSL',[0.33333,1,0.14]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0.33333,0.35,0.91]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'blue': {'left': Color('HSL',[0.66,1,0.14]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0.66,0.35,0.91]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'seaweed': {'left': Color('HSL',[0.78,1.0,0.1]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0.40000,0.1,0.9]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'bluebrown': {'left': Color('HSL',[0.65,0.53,0.49]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0.11,0.75,0.38]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'redgreen': {'left': Color('HSL',[0.97,0.96,0.36]),
|
2017-08-29 01:09:35 +05:30
|
|
|
'right': Color('HSL',[0.33333,1.0,0.14]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'bluered': {'left': Color('HSL',[0.65,0.53,0.49]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0.97,0.96,0.36]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2016-04-11 23:17:07 +05:30
|
|
|
'blueredrainbow':{'left': Color('HSL',[2.0/3.0,1,0.5]),
|
2016-03-04 22:23:55 +05:30
|
|
|
'right': Color('HSL',[0,1,0.5]),
|
|
|
|
'interpolate': 'linear' },
|
2016-04-11 23:17:07 +05:30
|
|
|
'orientation': {'left': Color('RGB',[0.933334,0.878432,0.878431]),
|
|
|
|
'right': Color('RGB',[0.250980,0.007843,0.000000]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
|
|
|
'strain': {'left': Color('RGB',[0.941177,0.941177,0.870588]),
|
|
|
|
'right': Color('RGB',[0.266667,0.266667,0.000000]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
|
|
|
'stress': {'left': Color('RGB',[0.878432,0.874511,0.949019]),
|
|
|
|
'right': Color('RGB',[0.000002,0.000000,0.286275]),
|
|
|
|
'interpolate': 'perceptualuniform'},
|
2013-05-28 19:34:25 +05:30
|
|
|
}
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2013-01-09 00:17:44 +05:30
|
|
|
def __init__(self,
|
|
|
|
left = Color('RGB',[1,1,1]),
|
|
|
|
right = Color('RGB',[0,0,0]),
|
2013-05-29 01:45:13 +05:30
|
|
|
interpolate = 'perceptualuniform',
|
2014-02-28 21:39:01 +05:30
|
|
|
predefined = None
|
2013-01-09 00:17:44 +05:30
|
|
|
):
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
|
|
|
Create a Colormap object.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
left : Color
|
|
|
|
left color (minimum value)
|
|
|
|
right : Color
|
|
|
|
right color (maximum value)
|
|
|
|
interpolate : str
|
|
|
|
interpolation scheme (either 'perceptualuniform' or 'linear')
|
|
|
|
predefined : bool
|
|
|
|
ignore other arguments and use predefined definition
|
2014-02-28 21:39:01 +05:30
|
|
|
|
2019-09-03 11:34:42 +05:30
|
|
|
"""
|
2016-04-11 23:17:07 +05:30
|
|
|
if predefined is not None:
|
2014-02-28 21:39:01 +05:30
|
|
|
left = self.__predefined__[predefined.lower()]['left']
|
|
|
|
right= self.__predefined__[predefined.lower()]['right']
|
|
|
|
interpolate = self.__predefined__[predefined.lower()]['interpolate']
|
|
|
|
|
2013-01-09 00:17:44 +05:30
|
|
|
if left.__class__.__name__ != 'Color':
|
|
|
|
left = Color()
|
|
|
|
if right.__class__.__name__ != 'Color':
|
|
|
|
right = Color()
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2013-05-28 23:03:51 +05:30
|
|
|
self.left = left
|
|
|
|
self.right = right
|
2013-05-29 01:45:13 +05:30
|
|
|
self.interpolate = interpolate
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2014-02-28 21:39:01 +05:30
|
|
|
def __repr__(self):
|
2019-09-02 23:39:03 +05:30
|
|
|
"""Left and right value of colormap."""
|
2014-02-28 21:39:01 +05:30
|
|
|
return 'Left: %s Right: %s'%(self.left,self.right)
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2016-03-04 22:23:55 +05:30
|
|
|
# ------------------------------------------------------------------
|
2014-02-28 21:39:01 +05:30
|
|
|
def invert(self):
|
2019-09-03 11:34:42 +05:30
|
|
|
"""Switch left/minimum with right/maximum."""
|
2014-02-28 21:39:01 +05:30
|
|
|
(self.left, self.right) = (self.right, self.left)
|
2013-05-28 19:20:36 +05:30
|
|
|
return self
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
|
2019-09-03 11:34:42 +05:30
|
|
|
# ------------------------------------------------------------------
|
|
|
|
def show_predefined(self):
|
|
|
|
"""Show the labels of the predefined colormaps."""
|
|
|
|
print('\n'.join(self.__predefined__.keys()))
|
|
|
|
|
2017-08-29 01:09:35 +05:30
|
|
|
# ------------------------------------------------------------------
|
2015-06-13 17:16:32 +05:30
|
|
|
def color(self,fraction = 0.5):
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2014-02-28 21:39:01 +05:30
|
|
|
def interpolate_Msh(lo, hi, frac):
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2013-02-03 01:47:02 +05:30
|
|
|
def rad_diff(a,b):
|
|
|
|
return abs(a[2]-b[2])
|
2017-08-29 01:09:35 +05:30
|
|
|
# if saturation of one of the two colors is too less than the other, hue of the less
|
2016-03-04 22:23:55 +05:30
|
|
|
def adjust_hue(Msh_sat, Msh_unsat):
|
2013-02-03 01:47:02 +05:30
|
|
|
if Msh_sat[0] >= Msh_unsat[0]:
|
|
|
|
return Msh_sat[2]
|
2013-01-15 23:54:10 +05:30
|
|
|
else:
|
2020-01-02 22:32:13 +05:30
|
|
|
hSpin = Msh_sat[1]/np.sin(Msh_sat[1])*np.sqrt(Msh_unsat[0]**2.0-Msh_sat[0]**2)/Msh_sat[0]
|
|
|
|
if Msh_sat[2] < - np.pi/3.0: hSpin *= -1.0
|
2013-02-03 01:47:02 +05:30
|
|
|
return Msh_sat[2] + hSpin
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
Msh1 = np.array(lo[:])
|
|
|
|
Msh2 = np.array(hi[:])
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2020-01-02 22:32:13 +05:30
|
|
|
if (Msh1[1] > 0.05 and Msh2[1] > 0.05 and rad_diff(Msh1,Msh2) > np.pi/3.0):
|
2013-02-03 01:47:02 +05:30
|
|
|
M_mid = max(Msh1[0],Msh2[0],88.0)
|
|
|
|
if frac < 0.5:
|
2015-06-13 17:16:32 +05:30
|
|
|
Msh2 = np.array([M_mid,0.0,0.0],'d')
|
2014-02-28 21:39:01 +05:30
|
|
|
frac *= 2.0
|
2013-01-15 23:54:10 +05:30
|
|
|
else:
|
2015-06-13 17:16:32 +05:30
|
|
|
Msh1 = np.array([M_mid,0.0,0.0],'d')
|
2013-02-03 01:47:02 +05:30
|
|
|
frac = 2.0*frac - 1.0
|
|
|
|
if Msh1[1] < 0.05 and Msh2[1] > 0.05: Msh1[2] = adjust_hue(Msh2,Msh1)
|
|
|
|
elif Msh1[1] > 0.05 and Msh2[1] < 0.05: Msh2[2] = adjust_hue(Msh1,Msh2)
|
2014-02-28 21:39:01 +05:30
|
|
|
Msh = (1.0 - frac) * Msh1 + frac * Msh2
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2014-02-28 21:39:01 +05:30
|
|
|
return Color('MSH',Msh)
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2014-02-28 21:39:01 +05:30
|
|
|
def interpolate_linear(lo, hi, frac):
|
2016-10-31 20:37:30 +05:30
|
|
|
"""Linear interpolation between lo and hi color at given fraction; output in model of lo color."""
|
2015-06-15 21:06:58 +05:30
|
|
|
interpolation = (1.0 - frac) * np.array(lo.color[:]) \
|
|
|
|
+ frac * np.array(hi.expressAs(lo.model).color[:])
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2014-02-28 21:39:01 +05:30
|
|
|
return Color(lo.model,interpolation)
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2014-02-28 21:39:01 +05:30
|
|
|
if self.interpolate == 'perceptualuniform':
|
2015-06-13 17:16:32 +05:30
|
|
|
return interpolate_Msh(self.left.expressAs('MSH').color,
|
|
|
|
self.right.expressAs('MSH').color,fraction)
|
2014-02-28 21:39:01 +05:30
|
|
|
elif self.interpolate == 'linear':
|
2015-06-13 17:16:32 +05:30
|
|
|
return interpolate_linear(self.left,
|
|
|
|
self.right,fraction)
|
2014-02-28 21:39:01 +05:30
|
|
|
else:
|
2015-06-13 17:16:32 +05:30
|
|
|
raise NameError('unknown color interpolation method')
|
2017-08-29 01:09:35 +05:30
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2015-06-13 17:16:32 +05:30
|
|
|
def export(self,name = 'uniformPerceptualColorMap',\
|
|
|
|
format = 'paraview',\
|
|
|
|
steps = 2,\
|
|
|
|
crop = [-1.0,1.0],
|
|
|
|
model = 'RGB'):
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
2015-06-13 17:16:32 +05:30
|
|
|
[RGB] colormap for use in paraview or gmsh, or as raw string, or array.
|
2016-03-04 22:23:55 +05:30
|
|
|
|
2016-10-31 19:03:23 +05:30
|
|
|
Arguments: name, format, steps, crop.
|
|
|
|
Format is one of (paraview, gmsh, raw, list).
|
|
|
|
Crop selects a (sub)range in [-1.0,1.0].
|
|
|
|
Generates sequential map if one limiting color is either white or black,
|
2015-06-13 17:16:32 +05:30
|
|
|
diverging map otherwise.
|
2016-03-04 22:23:55 +05:30
|
|
|
"""
|
|
|
|
format = format.lower() # consistent comparison basis
|
|
|
|
frac = 0.5*(np.array(crop) + 1.0) # rescale crop range to fractions
|
2016-07-02 16:58:32 +05:30
|
|
|
colors = [self.color(float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]).expressAs(model).color for i in range(steps)]
|
2015-06-13 17:16:32 +05:30
|
|
|
if format == 'paraview':
|
2019-09-02 23:21:32 +05:30
|
|
|
colormap = ['[\n {{\n "ColorSpace": "RGB", "Name": "{}", "DefaultMap": true,\n "RGBPoints" : ['.format(name)] \
|
2019-09-03 11:34:42 +05:30
|
|
|
+ [' {:4d},{:8.6f},{:8.6f},{:8.6f},'.format(i,color[0],color[1],color[2],) \
|
|
|
|
for i,color in enumerate(colors[:-1])] \
|
|
|
|
+ [' {:4d},{:8.6f},{:8.6f},{:8.6f} '.format(len(colors),colors[-1][0],colors[-1][1],colors[-1][2],)] \
|
2016-04-11 23:17:07 +05:30
|
|
|
+ [' ]\n }\n]']
|
2019-09-03 11:34:42 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
elif format == 'gmsh':
|
|
|
|
colormap = ['View.ColorTable = {'] \
|
2016-07-02 16:58:32 +05:30
|
|
|
+ [',\n'.join(['{%s}'%(','.join([str(x*255.0) for x in color])) for color in colors])] \
|
2015-06-13 17:16:32 +05:30
|
|
|
+ ['}']
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
elif format == 'gom':
|
2019-09-02 23:39:03 +05:30
|
|
|
colormap = ['1 1 ' + str(name)
|
|
|
|
+ ' 9 ' + str(name)
|
2019-09-03 12:12:34 +05:30
|
|
|
+ ' 0 1 0 3 0 0 -1 9 \\ 0 0 0 255 255 255 0 0 255 '
|
2019-09-02 23:39:03 +05:30
|
|
|
+ '30 NO_UNIT 1 1 64 64 64 255 1 0 0 0 0 0 0 3 0 ' + str(len(colors))
|
2016-07-02 16:58:32 +05:30
|
|
|
+ ' '.join([' 0 %s 255 1'%(' '.join([str(int(x*255.0)) for x in color])) for color in reversed(colors)])]
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
elif format == 'raw':
|
|
|
|
colormap = ['\t'.join(map(str,color)) for color in colors]
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
elif format == 'list':
|
|
|
|
colormap = colors
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
else:
|
|
|
|
raise NameError('unknown color export format')
|
2017-08-29 01:09:35 +05:30
|
|
|
|
2015-06-13 17:16:32 +05:30
|
|
|
return '\n'.join(colormap) + '\n' if type(colormap[0]) is str else colormap
|