예제 #1
0
def findNonWhiteColors(N=924):
    for i in xrange(0, N):
        color = gROOT.GetColor(i)
        if i == kWhite:
            yield kWhite, color
        if color == None or getRGB(color) == (0.0, 0.0, 0.0):
            continue
        yield i, color
예제 #2
0
def getColorString(color):
    if gROOT.GetColor(color) != None:
        name = gROOT.GetColor(color).GetName()
        if name[0] == 'k':
            return name
    #imin = -1
    #dmin = 51;
    #cmin = 'kWhite';
    #for colkey, coli in color_dict.iteritems():
    #  diff = color - coli
    #  name = gROOT.GetColor(imin).GetName()
    #  if diff==0:
    #    return colkey;
    #  if abs(diff)<abs(dmin):
    #    imin = coli
    #    dmin = diff
    #    cmin = colkey;
    #if abs(dmin)<51 and gROOT.GetColor(imin)!=None:
    #  return "%s%+d"%(cmin,dmin)
    return "%s" % color
예제 #3
0
def findClosestExistingColor(*args, **kwargs):
    r, g, b = args[:3] if len(args) > 2 else getRGB(gROOT.GetColor(args[0]))
    cmax = kwargs.get('N', 924)
    dmin = 999
    imin = -1
    cmin = None
    color = None
    for i, col in findNonWhiteColors(cmax):
        r2, g2, b2 = getRGB(col)
        rbgdiff = dRGB(r, g, b, r2, g2, b2)
        if rbgdiff < dmin:
            #print "      (%.3f,%.3f,%.3f) vs. %4d, (%.3f,%.3f,%.3f), %5.4f"%(r,g,b,imin,r2,g2,b2,rbgdiff)
            imin, cmin, dmin = i, col, rbgdiff
    return imin, cmin, dmin
예제 #4
0
 def color(self, color, opacity=0):
     tcolor = gROOT.GetColor(color)
     a = tcolor.GetAlpha()
     sfx = ''
     alpha = ''
     opacity = int(opacity)
     if opacity <= 0 or opacity > 100: opacity = 15
     if opacity > 0 and opacity <= 100:
         ofactor = (100 - opacity) / 100.
         a = a * ofactor
         sfx = 'a'
         alpha = ',%.1f' % a
     else:
         a = ''
     hxcolor = tcolor.AsHexString()[1:]
     return "rgb%s(%s,%s,%s%s)" % (sfx, int(
         hxcolor[0:2], 16), int(hxcolor[2:4], 16), int(hxcolor[4:6],
                                                       16), alpha)
예제 #5
0
def getRGBString(color):
    if isinstance(color, int):
        color = gROOT.GetColor(color)
    if not color: return "255,255,255"
    return "%d,%d,%d" % (color.GetRed() * 255, color.GetGreen() * 255,
                         color.GetBlue() * 255)
예제 #6
0
파일: style.py 프로젝트: jhegeman/rootpy
def convert_color(color, mode):
    """
    Convert *color* to a TColor if *mode='root'* or to (r,g,b,a) if 'mpl'.

    The *color* argument can be a ROOT TColor or color index, an *RGB*
    or *RGBA* sequence or a string in any of several forms:

        1) a letter from the set 'rgbcmykw'
        2) a hex color string, like '#00FFFF'
        3) a standard name, like 'aqua'
        4) a float, like '0.4', indicating gray on a 0-1 scale

    if *arg* is *RGBA*, the *A* will simply be discarded.

    >>> convert_color(2)
    (1.0, 0.0, 0.0)
    >>> convert_color('b')
    (0.0, 0.0, 1.0)
    >>> convert_color('blue')
    (0.0, 0.0, 1.0)
    >>> convert_color('0.25')
    (0.25, 0.25, 0.25)
    """
    mode = mode.lower()
    if mode != 'mpl' and mode != 'root':
        raise ValueError("%s is not an understood value for mode" % mode)
    # if color is None:
    #     return None
    # elif color == 'none' or color == 'None':
    #     return 'none'
    # temp fix. needs improvement.
    if __use_matplotlib:
        try:  # color is a TColor
            _color = TColor(color)
            rgb = _color.GetRed(), _color.GetGreen(), _color.GetBlue()
            return convert_color(rgb, mode)
        except (TypeError, ReferenceError):
            pass
        try:  # color is a ROOT color index
            _color = gROOT.GetColor(color)
            rgb = _color.GetRed(), _color.GetGreen(), _color.GetBlue()
            return convert_color(rgb, mode)
        except (TypeError, ReferenceError):
            pass
        try:  # color is an (r,g,b) tuple from 0 to 255
            if max(color) > 1.:
                color = [x / 255. for x in color][0:3]
        except TypeError:
            pass
        # color is something understood by matplotlib
        color = colorConverter.to_rgb(color)
        if mode == 'root':
            return TColor.GetColor(*color)
    else:  # fall back on internal conversion
        if color in __colors.values():
            return color
        if color in __colors:
            color = __colors[color]
        elif type(color) is int:
            return color
        else:
            raise ValueError("Color %s is not understood" % repr(color))
    return color