示例#1
0
    def format_info(self, color):
        """ Format the selected color info """
        rgba = RGBA(color)

        try:
            web_color = webcolors.hex_to_name(color)
        except:
            web_color = None

        info = ['<h1 class="header">%s</h1>' % color]
        if web_color is not None:
            info.append('<strong>%s</strong><br><br>' % web_color)
        info.append('<a href="__palettes__">%s</a><br><br>' %
                    color_box(color, border_color, size=64))
        info.append('<span class="key">r:</span> %d ' % rgba.r +
                    '<span class="key">g:</span> %d ' % rgba.g +
                    '<span class="key">b:</span> %d<br>' % rgba.b)
        h, s, v = rgba.tohsv()
        info.append('<span class="key">h:</span> %.0f ' % (h * 360.0) +
                    '<span class="key">s:</span> %.0f ' % (s * 100.0) +
                    '<span class="key">v:</span> %.0f<br>' % (v * 100.0))
        h, l, s = rgba.tohls()
        info.append('<span class="key">h:</span> %.0f ' % (h * 360.0) +
                    '<span class="key">s:</span> %.0f ' % (s * 100.0) +
                    '<span class="key">l:</span> %.0f<br>' % (l * 100.0))
        return ''.join(info)
示例#2
0
    def format_info(self, color):
        """ Format the selected color info """
        rgba = RGBA(color)

        try:
            web_color = webcolors.hex_to_name(color)
        except:
            web_color = None

        info = ['<h1 class="header">%s</h1>' % color]
        if web_color is not None:
            info.append('<strong>%s</strong><br><br>' % web_color)
        info.append('<a href="__palettes__">%s</a><br><br>' % color_box(color, border_color, size=64))
        info.append(
            '<span class="key">r:</span> %d ' % rgba.r +
            '<span class="key">g:</span> %d ' % rgba.g +
            '<span class="key">b:</span> %d<br>' % rgba.b
        )
        h, s, v = rgba.tohsv()
        info.append(
            '<span class="key">h:</span> %.0f ' % (h * 360.0) +
            '<span class="key">s:</span> %.0f ' % (s * 100.0) +
            '<span class="key">v:</span> %.0f<br>' % (v * 100.0)
        )
        h, l, s = rgba.tohls()
        info.append(
            '<span class="key">h:</span> %.0f ' % (h * 360.0) +
            '<span class="key">s:</span> %.0f ' % (s * 100.0) +
            '<span class="key">l:</span> %.0f<br>' % (l * 100.0)
        )
        return ''.join(info)
示例#3
0
    def insert_color(self, target_color, convert=None):
        """Insert colors."""

        sels = self.view.sel()
        if (len(sels) == 1 and sels[0].size() == 0):
            point = sels[0].begin()
            insert_calc = InsertionCalc(self.view, point, target_color, convert)
            insert_calc.calc()
            if insert_calc.web_color:
                value = insert_calc.web_color
            elif insert_calc.convert_rgb:
                value = "%d, %d, %d" % (
                    int(target_color[1:3], 16),
                    int(target_color[3:5], 16),
                    int(target_color[5:7], 16)
                )
                if insert_calc.alpha:
                    value += ', %s' % insert_calc.alpha
                if insert_calc.format_override:
                    value = ("rgba(%s)" if insert_calc.alpha else "rgb(%s)") % value
            elif insert_calc.convert_hsl:
                hsl = RGBA(target_color)
                h, l, s = hsl.tohls()
                value = "%s, %s%%, %s%%" % (
                    util.fmt_float(h * 360.0),
                    util.fmt_float(s * 100.0),
                    util.fmt_float(l * 100.0)
                )
                if insert_calc.alpha:
                    value += ', %s' % insert_calc.alpha
                if insert_calc.format_override:
                    value = ("hsla(%s)" if insert_calc.alpha else "hsl(%s)") % value
            else:
                use_upper = ch_settings.get("upper_case_hex", False)
                value = target_color.upper() if use_upper else target_color.lower()
            self.view.sel().subtract(sels[0])
            self.view.sel().add(insert_calc.region)
            self.view.run_command("insert", {"characters": value})
        self.view.hide_popup()
示例#4
0
def translate_color(m, use_hex_argb=False, decode=False):
    """Translate the match object to a color w/ alpha."""

    color = None
    alpha = None
    alpha_dec = None
    if m.group('hex_compressed'):
        if decode:
            content = m.group('hex_compressed_content').decode('utf-8')
        else:
            content = m.group('hex_compressed_content')
        color = "#%02x%02x%02x" % (
            int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
        )
    elif m.group('hexa_compressed') and use_hex_argb:
        if decode:
            content = m.group('hexa_compressed_content').decode('utf-8')
        else:
            content = m.group('hexa_compressed_content')
        color = "#%02x%02x%02x" % (
            int(content[1:2] * 2, 16), int(content[2:3] * 2, 16), int(content[3:] * 2, 16)
        )
        alpha = content[0:1]
        alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hexa_compressed'):
        if decode:
            content = m.group('hexa_compressed_content').decode('utf-8')
        else:
            content = m.group('hexa_compressed_content')
        color = "#%02x%02x%02x" % (
            int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
        )
        alpha = content[3:]
        alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hex'):
        if decode:
            content = m.group('hex_content').decode('utf-8')
        else:
            content = m.group('hex_content')
        if len(content) == 6:
            color = "#%02x%02x%02x" % (
                int(content[0:2], 16), int(content[2:4], 16), int(content[4:6], 16)
            )
        else:
            color = "#%02x%02x%02x" % (
                int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
            )
    elif m.group('hexa') and use_hex_argb:
        if decode:
            content = m.group('hexa_content').decode('utf-8')
        else:
            content = m.group('hexa_content')
        if len(content) == 8:
            color = "#%02x%02x%02x" % (
                int(content[2:4], 16), int(content[4:6], 16), int(content[6:], 16)
            )
            alpha = content[0:2]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
        else:
            color = "#%02x%02x%02x" % (
                int(content[1:2] * 2, 16), int(content[2:3] * 2, 16), int(content[3:] * 2, 16)
            )
            alpha = content[0:1]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hexa'):
        if decode:
            content = m.group('hexa_content').decode('utf-8')
        else:
            content = m.group('hexa_content')
        if len(content) == 8:
            color = "#%02x%02x%02x" % (
                int(content[0:2], 16), int(content[2:4], 16), int(content[4:6], 16)
            )
            alpha = content[6:]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
        else:
            color = "#%02x%02x%02x" % (
                int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
            )
            alpha = content[3:]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('rgb'):
        if decode:
            content = [x.strip() for x in m.group('rgb_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('rgb_content').split(',')]
        if content[0].endswith('%'):
            r = round_int(clamp(float(content[0].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            g = round_int(clamp(float(content[1].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            b = round_int(clamp(float(content[2].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            color = "#%02x%02x%02x" % (r, g, b)
        else:
            color = "#%02x%02x%02x" % (
                clamp(round_int(float(content[0])), 0, 255),
                clamp(round_int(float(content[1])), 0, 255),
                clamp(round_int(float(content[2])), 0, 255)
            )
    elif m.group('rgba'):
        if decode:
            content = [x.strip() for x in m.group('rgba_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('rgba_content').split(',')]
        if content[0].endswith('%'):
            r = round_int(clamp(float(content[0].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            g = round_int(clamp(float(content[1].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            b = round_int(clamp(float(content[2].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
            color = "#%02x%02x%02x" % (r, g, b)
        else:
            color = "#%02x%02x%02x" % (
                clamp(round_int(float(content[0])), 0, 255),
                clamp(round_int(float(content[1])), 0, 255),
                clamp(round_int(float(content[2])), 0, 255)
            )
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('gray'):
        if decode:
            content = m.group('gray_content').decode('utf-8')
        else:
            content = m.group('gray_content')
        if content.endswith('%'):
            g = round_int(clamp(float(content.strip('%')), 0.0, 255.0) * (255.0 / 100.0))
        else:
            g = clamp(round_int(float(content)), 0, 255)
        color = "#%02x%02x%02x" % (g, g, g)
    elif m.group('graya'):
        if decode:
            content = [x.strip() for x in m.group('graya_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('graya_content').split(',')]
        if content[0].endswith('%'):
            g = round_int(clamp(float(content[0].strip('%')), 0.0, 255.0) * (255.0 / 100.0))
        else:
            g = clamp(round_int(float(content[0])), 0, 255)
        color = "#%02x%02x%02x" % (g, g, g)
        if content[1].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[1])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[1])
    elif m.group('hsl'):
        if decode:
            content = [x.strip() for x in m.group('hsl_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hsl_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        s = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        l = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
    elif m.group('hsla'):
        if decode:
            content = [x.strip() for x in m.group('hsla_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hsla_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        s = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        l = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('hwb'):
        if decode:
            content = [x.strip() for x in m.group('hwb_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hwb_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        w = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        b = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhwb(h, w, b)
        color = rgba.get_rgb()
    elif m.group('hwba'):
        if decode:
            content = [x.strip() for x in m.group('hwba_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hwba_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        w = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        b = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhwb(h, w, b)
        color = rgba.get_rgb()
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('webcolors'):
        try:
            if decode:
                color = csscolors.name2hex(m.group('webcolors').decode('utf-8')).lower()
            else:
                color = csscolors.name2hex(m.group('webcolors')).lower()
        except:
            pass
    return color, alpha, alpha_dec
示例#5
0
    def format_info(self, color, alpha=None):
        """Format the selected color info."""
        rgba = RGBA(color)

        try:
            web_color = csscolors.hex2name(rgba.get_rgb())
        except Exception:
            web_color = None

        color_picker = util.color_picker_available()
        s = sublime.load_settings('color_helper.sublime-settings')
        show_global_palettes = s.get('enable_global_user_palettes', True)
        show_project_palettes = s.get('enable_project_user_palettes', True)
        show_favorite_palette = s.get('enable_favorite_palette', True)
        show_current_palette = s.get('enable_current_file_palette', True)
        show_conversions = s.get('enable_color_conversions', True)
        show_picker = s.get('enable_color_picker', True)
        palettes_enabled = (
            show_global_palettes or show_project_palettes or
            show_favorite_palette or show_current_palette
        )
        click_color_box_to_pick = s.get('click_color_box_to_pick', 'none')

        if click_color_box_to_pick == 'color_picker' and color_picker and show_picker:
            color_box_wrapper = '\n\n[%s]' + ('(__color_picker__:%s)' % color)
        elif click_color_box_to_pick == 'palette_picker' and palettes_enabled:
            color_box_wrapper = '\n\n[%s](__palettes__)'
        else:
            color_box_wrapper = '\n\n%s'

        info = []

        if click_color_box_to_pick != 'palette_picker' and palettes_enabled:
            info.append(PALETTE_MENU)
        if click_color_box_to_pick != 'color_picker' and color_picker and show_picker:
            info.append(PICK_MENU % color)
        if show_global_palettes or show_project_palettes:
            info.append(ADD_COLOR_MENU % color.lower())
        if show_favorite_palette:
            if color in util.get_favs()['colors']:
                info.append(UNMARK_MENU % color.lower())
            else:
                info.append(MARK_MENU % color.lower())

        info.append(
            color_box_wrapper % mdpopups.color_box([color], '#cccccc', '#333333', height=64, width=192, border_size=2)
        )

        if show_conversions:
            info.append('\n\n---\n\n')
            if web_color:
                info.append(WEB_COLOR % (color, web_color))
            info.append(HEX_COLOR % (color, (color.lower() if not alpha else color[:-2].lower())))
            info.append(RGB_COLOR % (color, rgba.r, rgba.g, rgba.b))
            info.append(RGBA_COLOR % (color, rgba.r, rgba.g, rgba.b, alpha if alpha else '1'))
            h, l, s = rgba.tohls()
            info.append(
                HSL_COLOR % (color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0))
            )
            info.append(
                HSLA_COLOR % (
                    color, util.fmt_float(h * 360.0), util.fmt_float(s * 100.0), util.fmt_float(l * 100.0),
                    alpha if alpha else '1'
                )
            )
        return ''.join(info)
def translate_color(m, decode=False):
    """Translate the match object to a color w/ alpha."""

    color = None
    alpha = None
    if m.group('hex'):
        if decode:
            content = m.group('hex_content')
        else:
            content = m.group('hex_content')
        if len(content) == 6:
            color = "#%02x%02x%02x" % (
                int(content[0:2], 16), int(content[2:4], 16), int(content[4:6], 16)
            )
        else:
            color = "#%02x%02x%02x" % (
                int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
            )
    elif m.group('rgb'):
        if decode:
            content = [x.strip() for x in m.group('rgb_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('rgb_content').split(',')]
        color = "#%02x%02x%02x" % (
            int(content[0]), int(content[1]), int(content[2])
        )
    elif m.group('rgba'):
        if decode:
            content = [x.strip() for x in m.group('rgba_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('rgba_content').split(',')]
        color = "#%02x%02x%02x" % (
            int(content[0]), int(content[1]), int(content[2])
        )
        alpha = content[3]
    elif m.group('hsl'):
        if decode:
            content = [x.strip() for x in m.group('hsl_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hsl_content').split(',')]
        rgba = RGBA()
        h = float(content[0]) / 360.0
        s = float(content[1].strip('%')) / 100.0
        l = float(content[2].strip('%')) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
    elif m.group('hsla'):
        if decode:
            content = [x.strip() for x in m.group('hsla_content').decode('utf-8').split(',')]
        else:
            content = [x.strip() for x in m.group('hsla_content').split(',')]
        rgba = RGBA()
        h = float(content[0]) / 360.0
        s = float(content[1].strip('%')) / 100.0
        l = float(content[2].strip('%')) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
        alpha = content[3]
    elif m.group('webcolors'):
        try:
            if decode:
                color = csscolors.name2hex(m.group('webcolors').decode('utf-8')).lower()
            else:
                color = csscolors.name2hex(m.group('webcolors')).lower()
        except:
            pass
    return color, alpha
示例#7
0
def translate_color(m, use_hex_argb=False, decode=False):
    """Translate the match object to a color w/ alpha."""

    color = None
    alpha = None
    alpha_dec = None
    if m.group('hex_compressed'):
        if decode:
            content = m.group('hex_compressed_content').decode('utf-8')
        else:
            content = m.group('hex_compressed_content')
        color = "#%02x%02x%02x" % (int(content[0:1] * 2,
                                       16), int(content[1:2] * 2,
                                                16), int(content[2:3] * 2, 16))
    elif m.group('hexa_compressed') and use_hex_argb:
        if decode:
            content = m.group('hexa_compressed_content').decode('utf-8')
        else:
            content = m.group('hexa_compressed_content')
        color = "#%02x%02x%02x" % (int(content[1:2] * 2,
                                       16), int(content[2:3] * 2,
                                                16), int(content[3:] * 2, 16))
        alpha = content[0:1]
        alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hexa_compressed'):
        if decode:
            content = m.group('hexa_compressed_content').decode('utf-8')
        else:
            content = m.group('hexa_compressed_content')
        color = "#%02x%02x%02x" % (int(content[0:1] * 2,
                                       16), int(content[1:2] * 2,
                                                16), int(content[2:3] * 2, 16))
        alpha = content[3:]
        alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hex'):
        if decode:
            content = m.group('hex_content').decode('utf-8')
        else:
            content = m.group('hex_content')
        if len(content) == 6:
            color = "#%02x%02x%02x" % (int(
                content[0:2], 16), int(content[2:4], 16), int(
                    content[4:6], 16))
        else:
            color = "#%02x%02x%02x" % (int(
                content[0:1] * 2, 16), int(content[1:2] * 2,
                                           16), int(content[2:3] * 2, 16))
    elif m.group('hexa') and use_hex_argb:
        if decode:
            content = m.group('hexa_content').decode('utf-8')
        else:
            content = m.group('hexa_content')
        if len(content) == 8:
            color = "#%02x%02x%02x" % (int(
                content[2:4], 16), int(content[4:6], 16), int(content[6:], 16))
            alpha = content[0:2]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
        else:
            color = "#%02x%02x%02x" % (int(
                content[1:2] * 2, 16), int(content[2:3] * 2,
                                           16), int(content[3:] * 2, 16))
            alpha = content[0:1]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('hexa'):
        if decode:
            content = m.group('hexa_content').decode('utf-8')
        else:
            content = m.group('hexa_content')
        if len(content) == 8:
            color = "#%02x%02x%02x" % (int(
                content[0:2], 16), int(content[2:4], 16), int(
                    content[4:6], 16))
            alpha = content[6:]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
        else:
            color = "#%02x%02x%02x" % (int(
                content[0:1] * 2, 16), int(content[1:2] * 2,
                                           16), int(content[2:3] * 2, 16))
            alpha = content[3:]
            alpha_dec = fmt_float(float(int(alpha, 16)) / 255.0, 3)
    elif m.group('rgb'):
        if decode:
            content = [
                x.strip()
                for x in m.group('rgb_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('rgb_content').split(',')]
        if content[0].endswith('%'):
            r = round_int(
                clamp(float(content[0].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            g = round_int(
                clamp(float(content[1].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            b = round_int(
                clamp(float(content[2].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            color = "#%02x%02x%02x" % (r, g, b)
        else:
            color = "#%02x%02x%02x" % (clamp(
                round_int(float(content[0])), 0,
                255), clamp(round_int(float(content[1])), 0,
                            255), clamp(round_int(float(content[2])), 0, 255))
    elif m.group('rgba'):
        if decode:
            content = [
                x.strip()
                for x in m.group('rgba_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('rgba_content').split(',')]
        if content[0].endswith('%'):
            r = round_int(
                clamp(float(content[0].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            g = round_int(
                clamp(float(content[1].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            b = round_int(
                clamp(float(content[2].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
            color = "#%02x%02x%02x" % (r, g, b)
        else:
            color = "#%02x%02x%02x" % (clamp(
                round_int(float(content[0])), 0,
                255), clamp(round_int(float(content[1])), 0,
                            255), clamp(round_int(float(content[2])), 0, 255))
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('gray'):
        if decode:
            content = m.group('gray_content').decode('utf-8')
        else:
            content = m.group('gray_content')
        if content.endswith('%'):
            g = round_int(
                clamp(float(content.strip('%')), 0.0, 255.0) * (255.0 / 100.0))
        else:
            g = clamp(round_int(float(content)), 0, 255)
        color = "#%02x%02x%02x" % (g, g, g)
    elif m.group('graya'):
        if decode:
            content = [
                x.strip()
                for x in m.group('graya_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('graya_content').split(',')]
        if content[0].endswith('%'):
            g = round_int(
                clamp(float(content[0].strip('%')), 0.0, 255.0) *
                (255.0 / 100.0))
        else:
            g = clamp(round_int(float(content[0])), 0, 255)
        color = "#%02x%02x%02x" % (g, g, g)
        if content[1].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[1])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[1])
    elif m.group('hsl'):
        if decode:
            content = [
                x.strip()
                for x in m.group('hsl_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('hsl_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        s = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        l = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
    elif m.group('hsla'):
        if decode:
            content = [
                x.strip()
                for x in m.group('hsla_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('hsla_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        s = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        l = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhls(h, l, s)
        color = rgba.get_rgb()
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('hwb'):
        if decode:
            content = [
                x.strip()
                for x in m.group('hwb_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('hwb_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        w = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        b = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhwb(h, w, b)
        color = rgba.get_rgb()
    elif m.group('hwba'):
        if decode:
            content = [
                x.strip()
                for x in m.group('hwba_content').decode('utf-8').split(',')
            ]
        else:
            content = [x.strip() for x in m.group('hwba_content').split(',')]
        rgba = RGBA()
        hue = float(content[0])
        if hue < 0.0 or hue > 360.0:
            hue = hue % 360.0
        h = hue / 360.0
        w = clamp(float(content[1].strip('%')), 0.0, 100.0) / 100.0
        b = clamp(float(content[2].strip('%')), 0.0, 100.0) / 100.0
        rgba.fromhwb(h, w, b)
        color = rgba.get_rgb()
        if content[3].endswith('%'):
            alpha, alpha_dec = alpha_percent_normalize(content[3])
        else:
            alpha, alpha_dec = alpha_dec_normalize(content[3])
    elif m.group('webcolors'):
        try:
            if decode:
                color = csscolors.name2hex(
                    m.group('webcolors').decode('utf-8')).lower()
            else:
                color = csscolors.name2hex(m.group('webcolors')).lower()
        except:
            pass
    return color, alpha, alpha_dec
示例#8
0
 def insert_color(self, target_color):
     sels = self.view.sel()
     if (len(sels) == 1 and sels[0].size() == 0):
         try:
             web_color = webcolors.hex_to_name(target_color)
         except:
             web_color = None
         point = sels[0].begin()
         visible = self.view.visible_region()
         start = point - 50
         end = point + 50
         convert_rgb = False
         convert_hsl = False
         alpha = None
         region = sublime.Region(point)
         if start < visible.begin():
             start = visible.begin()
         if end > visible.end():
             end = visible.end()
         bfr = self.view.substr(sublime.Region(start, end))
         ref = point - start
         found = False
         for m in COLOR_RE.finditer(bfr):
             if ref >= m.start(0) and ref < m.end(0):
                 found = True
                 if m.group('hex'):
                     region = sublime.Region(
                         m.start('hex') + start,
                         m.end('hex') + start)
                     break
                 elif m.group('rgb'):
                     if web_color:
                         region = sublime.Region(
                             m.start('rgb') + start,
                             m.end('rgb') + start)
                     else:
                         region = sublime.Region(
                             m.start('rgb_content') + start,
                             m.end('rgb_content') + start)
                         convert_rgb = True
                     break
                 elif m.group('rgba'):
                     web_color = None
                     region = sublime.Region(
                         m.start('rgba_content') + start,
                         m.end('rgba_content') + start)
                     convert_rgb = True
                     content = [
                         x.strip()
                         for x in m.group('rgba_content').split(',')
                     ]
                     alpha = content[3]
                     break
                 elif m.group('hsl'):
                     if web_color:
                         region = sublime.Region(
                             m.start('hsl') + start,
                             m.end('hsl') + start)
                     else:
                         region = sublime.Region(
                             m.start('hsl_content') + start,
                             m.end('hsl_content') + start)
                         convert_hsl = True
                     break
                 elif m.group('hsla'):
                     web_color = None
                     region = sublime.Region(
                         m.start('hsla_content') + start,
                         m.end('hsla_content') + start)
                     convert_hsl = True
                     content = [
                         x.strip().rstrip('%')
                         for x in m.group('hsla_content').split(',')
                     ]
                     alpha = content[3]
                     break
                 elif m.group('hash'):
                     region = sublime.Region(
                         m.start('hash') + start,
                         m.end('hash') + start)
                     break
                 elif m.group('rgb'):
                     if not web_color:
                         convert_rgb = True
                     break
                 elif m.group('rgba'):
                     convert_rgb = True
                     alpha = '1'
                     break
                 elif m.group('hsl'):
                     if not web_color:
                         convert_hsl = True
                     break
                 elif m.group('rgb'):
                     convert_hsl = True
                     alpha = '1'
                     break
                 else:
                     found = False
         if not found:
             word_region = self.view.word(sels[0])
             word = self.view.substr(word_region)
             try:
                 webcolors.name_to_hex(word).lower()
                 region = word_region
             except:
                 pass
         if web_color:
             value = web_color
         elif convert_rgb:
             value = "%d, %d, %d" % (int(
                 target_color[1:3], 16), int(
                     target_color[3:5], 16), int(target_color[5:7], 16))
             if alpha:
                 value += ', %s' % alpha
         elif convert_hsl:
             hsl = RGBA(target_color)
             h, l, s = hsl.tohls()
             value = "%d, %d%%, %d%%" % (int(
                 '%.0f' %
                 (h * 360.0)), int('%.0f' %
                                   (s * 100.0)), int('%.0f' % (l * 100.0)))
             if alpha:
                 value += ', %s' % alpha
         else:
             value = target_color
         self.view.sel().subtract(sels[0])
         self.view.sel().add(region)
         self.view.run_command("insert", {"characters": value})
     self.view.hide_popup()
示例#9
0
    def show_color_info(self, update=False):
        """ Show the color under the cursor """

        color = None
        sels = self.view.sel()
        if (len(sels) == 1 and sels[0].size() == 0):
            point = sels[0].begin()
            visible = self.view.visible_region()
            start = point - 50
            end = point + 50
            if start < visible.begin():
                start = visible.begin()
            if end > visible.end():
                end = visible.end()
            bfr = self.view.substr(sublime.Region(start, end))
            ref = point - start
            for m in COLOR_RE.finditer(bfr):
                if ref >= m.start(0) and ref < m.end(0):
                    if m.group('hex'):
                        content = m.group('hex_content')
                        if len(content) == 6:
                            color = "%02x%02x%02x" % (int(
                                content[0:2], 16), int(
                                    content[2:4], 16), int(content[4:6], 16))
                        else:
                            color = "%02x%02x%02x" % (int(
                                content[0:1] * 2, 16), int(
                                    content[1:2] * 2,
                                    16), int(content[2:3] * 2, 16))
                        break
                    elif m.group('rgb'):
                        content = [
                            x.strip()
                            for x in m.group('rgb_content').split(',')
                        ]
                        color = "%02x%02x%02x" % (int(
                            content[0]), int(content[1]), int(content[2]))
                        break
                    elif m.group('rgba'):
                        content = [
                            x.strip()
                            for x in m.group('rgba_content').split(',')
                        ]
                        color = "%02x%02x%02x%02x" % (
                            int(content[0]), int(content[1]), int(content[2]),
                            int('%.0f' % (float(content[3]) * 255.0)))
                        break
                    elif m.group('hsl'):
                        content = [
                            x.strip().rstrip('%')
                            for x in m.group('hsl_content').split(',')
                        ]
                        rgba = RGBA()
                        h = float(content[0]) / 360.0
                        s = float(content[1]) / 100.0
                        l = float(content[2]) / 100.0
                        rgba.fromhls(h, l, s)
                        color = rgba.get_rgb()[1:]
                        break
                    elif m.group('hsla'):
                        content = [
                            x.strip().rstrip('%')
                            for x in m.group('hsla_content').split(',')
                        ]
                        rgba = RGBA()
                        h = float(content[0]) / 360.0
                        s = float(content[1]) / 100.0
                        l = float(content[2]) / 100.0
                        rgba.fromhls(h, l, s)
                        color = rgba.get_rgb()[1:]
                        color += "%02X" % int('%.0f' %
                                              (float(content[3]) * 255.0))
                        break
            if color is None:
                word = self.view.substr(self.view.word(sels[0]))
                try:
                    color = webcolors.name_to_hex(word).lower()[1:]
                except:
                    pass
        if color is not None:
            html = [
                '<style>%s</style>' % (css if css is not None else '') +
                '<div class="content">' +
                # '<a href="__close__"><img style="width: 16px; height: 16px;" src="%s"></a>' % cross +
                self.format_info('#' + color.lower()) + '</div>'
            ]
            if update:
                self.view.update_popup(''.join(html))
            else:
                self.view.show_popup(''.join(html),
                                     location=-1,
                                     max_width=600,
                                     on_navigate=self.on_navigate)
        elif update:
            self.view.hide_popup()
示例#10
0
 def insert_color(self, target_color):
     sels = self.view.sel()
     if (len(sels) == 1 and sels[0].size() == 0):
         try:
             web_color = webcolors.hex_to_name(target_color)
         except:
             web_color = None
         point = sels[0].begin()
         visible = self.view.visible_region()
         start = point - 50
         end = point + 50
         convert_rgb = False
         convert_hsl = False
         alpha = None
         region = sublime.Region(point)
         if start < visible.begin():
             start = visible.begin()
         if end > visible.end():
             end = visible.end()
         bfr = self.view.substr(sublime.Region(start, end))
         ref = point - start
         found = False
         for m in COLOR_RE.finditer(bfr):
             if ref >= m.start(0) and ref < m.end(0):
                 found = True
                 if m.group('hex'):
                     region = sublime.Region(m.start('hex') + start, m.end('hex') + start)
                     break
                 elif m.group('rgb'):
                     if web_color:
                         region = sublime.Region(m.start('rgb') + start, m.end('rgb') + start)
                     else:
                         region = sublime.Region(m.start('rgb_content') + start, m.end('rgb_content') + start)
                         convert_rgb = True
                     break
                 elif m.group('rgba'):
                     web_color = None
                     region = sublime.Region(m.start('rgba_content') + start, m.end('rgba_content') + start)
                     convert_rgb = True
                     content = [x.strip() for x in m.group('rgba_content').split(',')]
                     alpha = content[3]
                     break
                 elif m.group('hsl'):
                     if web_color:
                         region = sublime.Region(m.start('hsl') + start, m.end('hsl') + start)
                     else:
                         region = sublime.Region(m.start('hsl_content') + start, m.end('hsl_content') + start)
                         convert_hsl = True
                     break
                 elif m.group('hsla'):
                     web_color = None
                     region = sublime.Region(m.start('hsla_content') + start, m.end('hsla_content') + start)
                     convert_hsl = True
                     content = [x.strip().rstrip('%') for x in m.group('hsla_content').split(',')]
                     alpha = content[3]
                     break
                 elif m.group('hash'):
                     region = sublime.Region(m.start('hash') + start, m.end('hash') + start)
                     break
                 elif m.group('rgb'):
                     if not web_color:
                         convert_rgb = True
                     break
                 elif m.group('rgba'):
                     convert_rgb = True
                     alpha = '1'
                     break
                 elif m.group('hsl'):
                     if not web_color:
                         convert_hsl = True
                     break
                 elif m.group('rgb'):
                     convert_hsl = True
                     alpha = '1'
                     break
                 else:
                     found = False
         if not found:
             word_region = self.view.word(sels[0])
             word = self.view.substr(word_region)
             try:
                 webcolors.name_to_hex(word).lower()
                 region = word_region
             except:
                 pass
         if web_color:
             value = web_color
         elif convert_rgb:
             value = "%d, %d, %d" % (
                 int(target_color[1:3], 16),
                 int(target_color[3:5], 16),
                 int(target_color[5:7], 16)
             )
             if alpha:
                 value += ', %s' % alpha
         elif convert_hsl:
             hsl = RGBA(target_color)
             h, l, s = hsl.tohls()
             value = "%d, %d%%, %d%%" % (
                 int('%.0f' % (h * 360.0)),
                 int('%.0f' % (s * 100.0)),
                 int('%.0f' % (l * 100.0))
             )
             if alpha:
                 value += ', %s' % alpha
         else:
             value = target_color
         self.view.sel().subtract(sels[0])
         self.view.sel().add(region)
         self.view.run_command("insert", {"characters": value})
     self.view.hide_popup()
示例#11
0
    def show_color_info(self, update=False):
        """ Show the color under the cursor """

        color = None
        sels = self.view.sel()
        if (len(sels) == 1 and sels[0].size() == 0):
            point = sels[0].begin()
            visible = self.view.visible_region()
            start = point - 50
            end = point + 50
            if start < visible.begin():
                start = visible.begin()
            if end > visible.end():
                end = visible.end()
            bfr = self.view.substr(sublime.Region(start, end))
            ref = point - start
            for m in COLOR_RE.finditer(bfr):
                if ref >= m.start(0) and ref < m.end(0):
                    if m.group('hex'):
                        content = m.group('hex_content')
                        if len(content) == 6:
                            color = "%02x%02x%02x" % (
                                int(content[0:2], 16), int(content[2:4], 16), int(content[4:6], 16)
                            )
                        else:
                            color = "%02x%02x%02x" % (
                                int(content[0:1] * 2, 16), int(content[1:2] * 2, 16), int(content[2:3] * 2, 16)
                            )
                        break
                    elif m.group('rgb'):
                        content = [x.strip() for x in m.group('rgb_content').split(',')]
                        color = "%02x%02x%02x" % (
                            int(content[0]), int(content[1]), int(content[2])
                        )
                        break
                    elif m.group('rgba'):
                        content = [x.strip() for x in m.group('rgba_content').split(',')]
                        color = "%02x%02x%02x%02x" % (
                            int(content[0]), int(content[1]), int(content[2]),
                            int('%.0f' % (float(content[3]) * 255.0))
                        )
                        break
                    elif m.group('hsl'):
                        content = [x.strip().rstrip('%') for x in m.group('hsl_content').split(',')]
                        rgba = RGBA()
                        h = float(content[0]) / 360.0
                        s = float(content[1]) / 100.0
                        l = float(content[2]) / 100.0
                        rgba.fromhls(h, l, s)
                        color = rgba.get_rgb()[1:]
                        break
                    elif m.group('hsla'):
                        content = [x.strip().rstrip('%') for x in m.group('hsla_content').split(',')]
                        rgba = RGBA()
                        h = float(content[0]) / 360.0
                        s = float(content[1]) / 100.0
                        l = float(content[2]) / 100.0
                        rgba.fromhls(h, l, s)
                        color = rgba.get_rgb()[1:]
                        color += "%02X" % int('%.0f' % (float(content[3]) * 255.0))
                        break
            if color is None:
                word = self.view.substr(self.view.word(sels[0]))
                try:
                    color = webcolors.name_to_hex(word).lower()[1:]
                except:
                    pass
        if color is not None:
            html = [
                '<style>%s</style>' % (css if css is not None else '') +
                '<div class="content">' +
                # '<a href="__close__"><img style="width: 16px; height: 16px;" src="%s"></a>' % cross +
                self.format_info('#' + color.lower()) +
                '</div>'
            ]
            if update:
                self.view.update_popup(''.join(html))
            else:
                self.view.show_popup(
                    ''.join(html), location=-1, max_width=600,
                    on_navigate=self.on_navigate
                )
        elif update:
            self.view.hide_popup()