Converts an palette index to the corresponding ANSI color.

            Arguments:
                index   - an int (from 0-15)
            Returns:
                index as str in a list for compatibility with values.
511594
def _index_to_ansi_values(self, index):
        ''' 
        '''
        if self.__class__.__name__[0] == 'F':   # Foreground
            if index < 8:
                index += ANSI_FG_LO_BASE
            else:
                index += (ANSI_FG_HI_BASE - 8)  # 82
        else:                                   # Background
            if index < 8:
                index += ANSI_BG_LO_BASE
            else:
                index += (ANSI_BG_HI_BASE - 8)  # 92
        return [str(index)]
415656
def int_to_rgba(cls, rgba_int):
        """"""
        if rgba_int is None:
            return None, None, None, None
        alpha = rgba_int % 256
        blue = rgba_int / 256 % 256
        green = rgba_int / 256 / 256 % 256
        red = rgba_int / 256 / 256 / 256 % 256
        return (red, green, blue, alpha)
1128136
def ansi(string, *args):
	

	ansi = ''

	for arg in args:
		arg = str(arg)
		if not re.match(ANSI_PATTERN, arg):
			raise ValueError('Additional arguments must be ansi strings')

		ansi += arg
	
	return ansi + string + colorama.Style.RESET_ALL
538444
def tuple_index(self, n):
        ''''''
        return n.args and n.args[-1].type == 'index' and general_type(n.args[-1].sequence.pseudo_type) == 'Tuple' and\
               n.args[-1].index.type == 'int' and\
               (n.args[-1].index.value == -1 or n.args[-1].index.value == len(n.args[-1].sequence.pseudo_type) - 2)
838816
def to_python_index(self, length, check_bounds=True, circular=False):
        
        if not self.is_unitless:
            raise ValueError("Index cannot have units: {0!r}".format(self))

        ret = int(self.value)
        if ret != self.value:
            raise ValueError("Index must be an integer: {0!r}".format(ret))

        if ret == 0:
            raise ValueError("Index cannot be zero")

        if check_bounds and not circular and abs(ret) > length:
            raise ValueError("Index {0!r} out of bounds for length {1}".format(ret, length))

        if ret > 0:
            ret -= 1

        if circular:
            ret = ret % length

        return ret
1356498
def get_bgcolor(self, index):
        """"""
        value = self.get_value(index)
        if index.column() < 3:
            color = ReadOnlyCollectionsModel.get_bgcolor(self, index)
        else:
            if self.remote:
                color_name = value['color']
            else:
                color_name = get_color_name(value)
            color = QColor(color_name)
            color.setAlphaF(.2)
        return color
954894
def _to_p(self, mode):
        

        if self.mode.endswith("A"):
            chans = self.channels[:-1]
            alpha = self.channels[-1]
            self._secondary_mode = self.mode[:-1]
        else:
            chans = self.channels
            alpha = None
            self._secondary_mode = self.mode

        palette = []
        selfmask = chans[0].mask
        for chn in chans[1:]:
            selfmask = np.ma.mask_or(selfmask, chn.mask)
        new_chn = np.ma.zeros(self.shape, dtype=int)
        color_nb = 0

        for i in range(self.height):
            for j in range(self.width):
                current_col = tuple([chn[i, j] for chn in chans])
                try:
                    next(idx
                         for idx in range(len(palette))
                         if palette[idx] == current_col)
                except StopIteration:
                    idx = color_nb
                    palette.append(current_col)
                    color_nb = color_nb + 1

                new_chn[i, j] = idx

        if self.fill_value is not None:
            if self.mode.endswith("A"):
                current_col = tuple(self.fill_value[:-1])
                fill_alpha = [self.fill_value[-1]]
            else:
                current_col = tuple(self.fill_value)
                fill_alpha = []
            try:
                next(idx
                     for idx in range(len(palette))
                     if palette[idx] == current_col)
            except StopIteration:
                idx = color_nb
                palette.append(current_col)
                color_nb = color_nb + 1

            self.fill_value = [idx] + fill_alpha

        new_chn.mask = selfmask
        self.palette = palette
        if alpha is None:
            self.channels = [new_chn]
        else:
            self.channels = [new_chn, alpha]
        self.mode = mode
869286
def interpolate_colors(colors, flat=False, num_colors=256):
    

    palette = []

    for i in range(num_colors):
        index = (i * (len(colors) - 1)) / (num_colors - 1.0)
        index_int = int(index)
        alpha = index - float(index_int)

        if alpha > 0:
            r = (1.0 - alpha) * colors[index_int][
                0] + alpha * colors[index_int + 1][0]
            g = (1.0 - alpha) * colors[index_int][
                1] + alpha * colors[index_int + 1][1]
            b = (1.0 - alpha) * colors[index_int][
                2] + alpha * colors[index_int + 1][2]
        else:
            r = (1.0 - alpha) * colors[index_int][0]
            g = (1.0 - alpha) * colors[index_int][1]
            b = (1.0 - alpha) * colors[index_int][2]

        if flat:
            palette.extend((int(r), int(g), int(b)))
        else:
            palette.append((int(r), int(g), int(b)))

    return palette
1326409
def color_(self, i=None):
        
        global palette, color_num
        if i is not None:
            color_num = i
        if color_num == len(palette) - 1:
            color_num = 0
        res = palette[color_num]
        color_num += 1
        return res
440090
def _find_colour(particle, start_index, screen_data):
        
        _, fg2, attr2, bg2 = screen_data
        index = start_index
        for i, colours in enumerate(particle.colours):
            if (fg2, attr2, bg2) == colours:
                index = i
                break
        return index
805658
def scolor(self):
        
        global palette
        color = palette[self.color_index]
        if len(palette) - 1 == self.color_index:
            self.color_index = 0
        else:
            self.color_index += 1
        self.color(color)
1086071
def byte2int(s, index=0):
    
    if six.PY2:
        return ord(s[index])
    return s[index]
3634
private void checkColorIndex(int index) {
    if (index < 0 || index >= mColors.length) {
      throw new IllegalArgumentException(String.format(Locale.US, "Index %d not valid", index));
    }
  }
1053032
def palette(fg, bg=-1):
    

    if not hasattr(palette, "counter"):
        palette.counter = 1

    if not hasattr(palette, "selections"):
        palette.selections = {}

    selection = "%s%s" % (str(fg), str(bg))
    if not selection in palette.selections:
        palette.selections[selection] = palette.counter
        palette.counter += 1

    # Get available colours
    colors = [c for c in dir(_curses) if c.startswith('COLOR')]
    if isinstance(fg, str):
        if not "COLOR_"+fg.upper() in colors:
            fg = -1
        else:
            fg = getattr(_curses, "COLOR_"+fg.upper())
    if isinstance(bg, str):
        if not "COLOR_"+bg.upper() in colors:
            bg = -1
        else:
            bg = getattr(_curses, "COLOR_"+bg.upper())

    _curses.init_pair(palette.selections[selection], fg, bg)
    return _curses.color_pair(palette.selections[selection])
276938
def color_array_by_hue_mix(value, palette):
    
    if int(value, 2) > 0:

        # Convert bits to list and reverse order to avoid issues with
        # differing lengths
        int_list = [int(i) for i in list(value[2:])]
        int_list.reverse()

        # since this is a 1D array, we need the zeroth elements
        # of np.nonzero.
        locs = np.nonzero(int_list)[0]
        # print(locs)
        # print(palette)
        rgb_vals = [palette[i] for i in locs]

        rgb = [0]*len(rgb_vals[0])  # We don't know if it's rgb or rgba
        for val in rgb_vals:
            for index in range(len(val)):
                rgb[index] += val[index]

        for i in range(len(rgb)):
            rgb[i] /= len(locs)

        return tuple(rgb)

    if int(value, 2) == 0:
        return (1, 1, 1) if len(palette[0]) == 3 else (1, 1, 1, 1)

    return -1
44985
def get_value_index(self, indices):
        
        size = self['size'] if self.get('size') else self['dimension']['size']
        ndims = len(size)
        mult = 1
        num = 0
        for idx, dim in enumerate(size):
            mult *= size[ndims - idx] if (idx > 0) else 1
            num += mult * indices[ndims - idx - 1]
        return num
1068650
def rgb_color_list_to_hex(color_list):
    
    color_list_rgb = [[int(x*255) for x in c[0:3]] for c in color_list]
    color_list_hex = ['#{:02X}{:02X}{:02X}'.format(rgb[0], rgb[1], rgb[2]) for rgb in color_list_rgb]
    return color_list_hex