def to_intensity(n): """Return intensity :param n: value between 0 and 1 :return: value between 0 and 255; round(n*127.5+127.5) """ check_range(n, 0, 1) return int(round(n * 127.5 + 127.5))
def hls2rgb(h, l, s, normalised=True): """Convert an HLS value to a RGB value. :param bool normalised: If *normalised* is True, the input HLS triplet should be in the range 0-1; otherwise, H in the range 0-360 and LS in the range 0-100. :return: the RGB triplet. The output triplet is in the range 0-1 whether the input is normalised or not. .. doctest:: >>> from colormap.colors import hls2rgb >>> hls2rgb(360, 50, 60, normalised=False) # doctest: +SKIP (0.8, 0.2, 0.2) .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` :func:`rgb2hex`, :func:`rgb2hsv`, :func:`hsv2rgb`, :func:`rgb2hls`, """ if normalised: upper = 1 else: upper = 100 if normalised: uppera = 1 else: uppera = 360 check_range(h, 0, uppera) check_range(s, 0, upper) check_range(l, 0, upper) if normalised == False: h, l, s = _normalise(h, l, s, mode="hls") return colorsys.hls_to_rgb(h, l, s)
def rgb2hsv(r, g, b, normalised=True): """Convert an RGB value to an HSV value. :param bool normalised: if *normalised* is True, the input RGB triplet should be in the range 0-1 (0-255 otherwise) :return: the HSV triplet. If *normalised* parameter is True, the output triplet is in the range 0-1; otherwise, H in the range 0-360 and LS in the range 0-100. .. doctest:: >>> from colormap.colors import rgb2hsv >>> rgb2hsv(0.5,0,1) (0.75, 1, 1) .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` :func:`rgb2hex`, :func:`hsv2rgb`, :func:`rgb2hls`, :func:`hls2rgb` """ # rgb_to_hsv expects normalised values ! if normalised: upper = 1 else: upper = 255 check_range(r, 0, upper) check_range(g, 0, upper) check_range(b, 0, upper) if normalised==False: r, g, b = _normalise(r, g, b) h, s, v = colorsys.rgb_to_hsv(r, g, b) return h,s,v
def hsv2rgb(h, s, v, normalised=True): """Convert a hue-saturation-value (HSV) value to a red-green-blue (RGB). :param bool normalised: If *normalised* is True, the input HSV triplet should be in the range 0-1; otherwise, H in the range 0-360 and LS in the range 0-100. :return: the RGB triplet. The output triplet is in the range 0-1 whether the input is normalised or not. .. doctest:: >>> from colormap.colors import hsv2rgb >>> hsv2rgb(0.5,1,1, normalised=True) # doctest: +SKIP (0, 1, 1) .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` :func:`rgb2hex`, :func:`rgb2hsv`, :func:`rgb2hls`, :func:`hls2rgb` .. seealso:: :func:`rgb2hex` """ if normalised: upper = 1 else: upper = 100 if normalised: uppera = 1 else: uppera = 360 check_range(h, 0, uppera) check_range(s, 0, upper) check_range(v, 0, upper) if normalised == False: h, s, v = _normalise(h, s, v, mode="hsv") return colorsys.hsv_to_rgb(h, s, v)
def rgb2hex(r, g, b, normalised=False): """Convert RGB to hexadecimal color :param: can be a tuple/list/set of 3 values (R,G,B) :return: a hex vesion ofthe RGB 3-tuple .. doctest:: >>> from colormap.colors import rgb2hex >>> rgb2hex(0,0,255, normalised=False) '#0000FF' >>> rgb2hex(0,0,1, normalised=True) '#0000FF' .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` , :func:`rgb2hsv`, :func:`hsv2rgb`, :func:`rgb2hls`, :func:`hls2rgb` """ if normalised: r, g, b = _denormalise(r, g, b, mode="rgb") check_range(r, 0, 255) check_range(g, 0, 255) check_range(b, 0, 255) return '#%02X%02X%02X' % (r, g, b)
def rgb2hsv(r, g, b, normalised=True): """Convert an RGB value to an HSV value. :param bool normalised: if *normalised* is True, the input RGB triplet should be in the range 0-1 (0-255 otherwise) :return: the HSV triplet. If *normalised* parameter is True, the output triplet is in the range 0-1; otherwise, H in the range 0-360 and LS in the range 0-100. .. doctest:: >>> from colormap.colors import rgb2hsv >>> rgb2hsv(0.5,0,1) (0.75, 1, 1) .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` :func:`rgb2hex`, :func:`hsv2rgb`, :func:`rgb2hls`, :func:`hls2rgb` """ # rgb_to_hsv expects normalised values ! if normalised: upper = 1 else: upper = 255 check_range(r, 0, upper) check_range(g, 0, upper) check_range(b, 0, upper) if normalised == False: r, g, b = _normalise(r, g, b) h, s, v = colorsys.rgb_to_hsv(r, g, b) return h, s, v
def rgb2hex(r, g, b, normalised=False): """Convert RGB to hexadecimal color :param: can be a tuple/list/set of 3 values (R,G,B) :return: a hex vesion ofthe RGB 3-tuple .. doctest:: >>> from colormap.colors import rgb2hex >>> rgb2hex(0,0,255, normalised=False) '#0000FF' >>> rgb2hex(0,0,1, normalised=True) '#0000FF' .. seealso:: :func:`hex2web`, :func:`web2hex`, :func:`hex2rgb` , :func:`rgb2hsv`, :func:`hsv2rgb`, :func:`rgb2hls`, :func:`hls2rgb` """ if normalised: r, g, b = _denormalise(r, g, b, mode="rgb") r = int(r) g = int(g) b = int(b) check_range(r, 0, 255) check_range(g, 0, 255) check_range(b, 0, 255) return '#%02X%02X%02X' % (r, g, b)
def yuv2rgb_int(y, u, v): """Convert YUV triplet into RGB `YUV <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 255 (not normalised) """ check_range(y, 0, 255) check_range(u, 0, 255) check_range(v, 0, 255) r = int(y + 1.13983 * v) g = int(y - 0.39465 * u - 0.58060 * v) b = int(y + 2.03211 * u) return (r, g, b)
def yuv2rgb(y, u, v): """Convert YUV triplet into RGB `YUV <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 255 (not normalised) """ check_range(y, 0, 1) check_range(u, 0, 1) check_range(v, 0, 1) A, B, C, D = 701.0 / 615.0, 25251.0 / 63983.0, 209599.0 / 361005.0, 443.0 / 218.0 r = y + A * v g = y - B * u - C * v b = y + D * u return (r, g, b)
def yuv2rgb(y, u, v): """Convert YUV triplet into RGB `YUV <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 255 (not normalised) """ check_range(y, 0,1) check_range(u, 0, 1) check_range(v, 0, 1) A, B, C, D = 701.0/615.0, 25251.0/63983.0, 209599.0/361005.0, 443.0/218.0 r = y + A * v g = y - B * u - C * v b = y + D * u return (r, g, b)
def rgb2yuv_int(r, g, b): """Convert RGB triplet into YUV `YUV wikipedia <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 255 (not normalised) """ check_range(r, 0, 255) check_range(g, 0, 255) check_range(b, 0, 255) y = int(0.299 * r + 0.587 * g + 0.114 * b) u = int(-32591.0 / 221500.0 * r + -63983.0 / 221500.0 * g + 0.436 * b) v = int(0.615 * r + -72201. / 140200 * g + -7011 / 70100. * b) return (y, u, v)
def rgb2yuv_int(r, g, b): """Convert RGB triplet into YUV `YUV wikipedia <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 255 (not normalised) """ check_range(r, 0, 255) check_range(g, 0, 255) check_range(b, 0, 255) y = int(0.299 * r + 0.587 * g + 0.114 * b) u = int(-32591.0/221500.0 * r + -63983.0/221500.0 * g + 0.436 * b) v = int(0.615 * r + -72201./140200 * g + -7011/70100. * b) return (y, u, v)
def rgb2yuv(r, g, b): """Convert RGB triplet into YUV :return: YUV triplet with values between 0 and 1 `YUV wikipedia <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 1 .. note:: the constants referenc used is Rec. 601 """ check_range(r, 0, 1) check_range(g, 0, 1) check_range(b, 0, 1) #y = int(0.299 * r + 0.587 * g + 0.114 * b) #u = int(-0.14713 * r + -0.28886 * g + 0.436 * b) #v = int(0.615 * r + -0.51499 * g + -0.10001 * b) y = 0.299 * r + 0.587 * g + 0.114 * b u = -32591.0 / 221500.0 * r + -63983.0 / 221500.0 * g + 0.436 * b v = 0.615 * r + -72201. / 140200 * g + -7011 / 70100. * b return (y, u, v)
def rgb2yuv(r, g, b): """Convert RGB triplet into YUV :return: YUV triplet with values between 0 and 1 `YUV wikipedia <http://en.wikipedia.org/wiki/YUV>`_ .. warning:: expected input must be between 0 and 1 .. note:: the constants referenc used is Rec. 601 """ check_range(r, 0, 1) check_range(g, 0, 1) check_range(b, 0, 1) #y = int(0.299 * r + 0.587 * g + 0.114 * b) #u = int(-0.14713 * r + -0.28886 * g + 0.436 * b) #v = int(0.615 * r + -0.51499 * g + -0.10001 * b) y = 0.299 * r + 0.587 * g + 0.114 * b u = -32591.0/221500.0 * r + -63983.0/221500.0 * g + 0.436 * b v = 0.615 * r + -72201./140200 * g + -7011/70100. * b return (y, u, v)
def test_check_range(): tools.check_range(1, 0, 1) tools.check_range(0, 0, 1) tools.check_range(0.5, 0, 1) try: tools.check_range(1, 0, 1, strict=True) assert False except: assert True try: tools.check_range(0, 0, 1, strict=True) assert False except: assert True try: tools.check_range(10, 0, 1, strict=False) assert False except: assert True try: tools.check_range(-10, 0, 1, strict=False) assert False except: assert True
def test_check_range(): tools.check_range(1, 0,1) tools.check_range(0, 0,1) tools.check_range(0.5, 0,1) try: tools.check_range(1, 0,1, strict=True) assert False except: assert True try: tools.check_range(0, 0,1, strict=True) assert False except: assert True try: tools.check_range(10, 0,1, strict=False) assert False except: assert True try: tools.check_range(-10, 0,1, strict=False) assert False except: assert True