示例#1
0
    def __init__(self, view, point, target_color, convert, allowed_colors,
                 use_hex_argb):
        """Initialize."""

        self.convert_rgb = False
        self.convert_hsl = False
        self.convert_hwb = False
        self.convert_gray = False
        self.allowed_colors = allowed_colors
        self.alpha = None
        self.force_alpha = False
        self.use_hex_argb = use_hex_argb
        self.alpha_hex = None
        self.view = view
        self.start = point - 50
        self.end = point + 50
        self.point = point
        self.region = sublime.Region(point)
        self.format_override = True

        visible = self.view.visible_region()
        if self.start < visible.begin():
            self.start = visible.begin()
        if self.end > visible.end():
            self.end = visible.end()
        self.web_color = None
        self.color = target_color[:-2] if len(
            target_color) > 7 else target_color

        if convert == "name":
            try:
                if len(target_color) > 7:
                    target_color = target_color[:-2]
                self.web_color = csscolors.hex2name(target_color)
            except:
                pass
            self.force_alpha = False
        elif convert in ('gray', 'graya'):
            self.convert_gray = True
            self.force_alpha = convert == 'graya'
        elif convert in ('hex', 'hexa', 'ahex'):
            self.force_alpha = convert in ('hexa', 'ahex')
        elif convert in ('rgb', 'rgba'):
            self.convert_rgb = True
            self.force_alpha = convert == 'rgba'
        elif convert in ('hsl', 'hsla'):
            self.convert_hsl = True
            self.force_alpha = convert == 'hsla'
        elif convert in ('hwb', 'hwba'):
            self.convert_hwb = True
            self.force_alpha = convert == 'hwba'
    def __init__(self, view, point, target_color, convert, allowed_colors, use_hex_argb):
        """Initialize."""

        self.convert_rgb = False
        self.convert_hsl = False
        self.convert_hwb = False
        self.convert_gray = False
        self.allowed_colors = allowed_colors
        self.alpha = None
        self.force_alpha = False
        self.use_hex_argb = use_hex_argb
        self.alpha_hex = None
        self.view = view
        self.start = point - 50
        self.end = point + 50
        self.point = point
        self.region = sublime.Region(point)
        self.format_override = True

        visible = self.view.visible_region()
        if self.start < visible.begin():
            self.start = visible.begin()
        if self.end > visible.end():
            self.end = visible.end()
        self.web_color = None
        self.color = target_color[:-2] if len(target_color) > 7 else target_color

        if convert == "name":
            try:
                if len(target_color) > 7:
                    target_color = target_color[:-2]
                self.web_color = csscolors.hex2name(target_color)
            except:
                pass
            self.force_alpha = True
        elif convert in ('gray', 'graya'):
            self.convert_gray = True
            self.force_alpha = convert == 'graya'
        elif convert in ('hex', 'hexa', 'ahex'):
            self.force_alpha = convert in ('hexa', 'ahex')
        elif convert in ('rgb', 'rgba'):
            self.convert_rgb = True
            self.force_alpha = convert == 'rgba'
        elif convert in ('hsl', 'hsla'):
            self.convert_hsl = True
            self.force_alpha = convert == 'hsla'
        elif convert in ('hwb', 'hwba'):
            self.convert_hwb = True
            self.force_alpha = convert == 'hwba'
    def __init__(self, view, point, target_color, convert=None):
        """Initialize insertion object."""

        ch_settings = sublime.load_settings('color_helper.sublime-settings')
        self.convert = '' if convert is None else convert
        self.view = view
        self.convert_rgb = False
        self.convert_hsl = False
        self.alpha = None
        self.region = sublime.Region(point)
        self.format_override = False
        self.start = point - 50
        self.end = point + 50
        self.point = point
        visible = self.view.visible_region()
        if self.start < visible.begin():
            self.start = visible.begin()
        if self.end > visible.end():
            self.end = visible.end()
        self.use_web_colors = bool(ch_settings.get('use_webcolor_names', True))
        self.preferred_format = ch_settings.get('preferred_format', 'hex')
        self.preferred_alpha_format = ch_settings.get('preferred_alpha_format', 'rgba')
        self.force_alpha = False
        if self.convert:
            self.format_override = True
            if self.convert == "name":
                self.use_web_colors = True
                if len(target_color) > 7:
                    target_color = target_color[:-2]
            elif self.convert == 'hex':
                self.convert_rgb = False
                self.use_web_colors = False
            elif self.convert in ('rgb', 'rgba'):
                self.convert_rgb = True
                self.force_alpha = self.convert == 'rgba'
                self.use_web_colors = False
            elif self.convert in ('hsl', 'hsla'):
                self.convert_hsl = True
                self.force_alpha = self.convert == 'hsla'
                self.use_web_colors = False

        self.target_color = target_color
        try:
            self.web_color = csscolors.hex2name(target_color) if self.use_web_colors else None
        except:
            self.web_color = None
示例#4
0
    def run(self,
            edit,
            color='#ffffff',
            allowed_colors=util.ALL,
            use_hex_argb=None,
            compress_hex=False,
            hsl=False,
            hirespick=None,
            colornames=False,
            on_done=None,
            on_cancel=None):
        """Run command."""

        self.on_done = on_done
        self.on_cancel = on_cancel
        self.use_hex_argb = use_hex_argb
        self.compress_hex = compress_hex
        self.allowed_colors = allowed_colors
        self.template_vars = {"legacy": util.LEGACY_CLASS}
        self.hex_map = sublime.load_settings(
            'color_helper.sublime-settings').get('use_hex_color_picker', True)
        rgba = util.RGBA(color)
        self.set_sizes()
        self.hsl = hsl
        self.color = rgba.get_rgba()
        self.alpha = util.fmt_float(float(int(self.color[-2:], 16)) / 255.0, 3)
        try:
            self.web_color = csscolors.hex2name(rgba.get_rgb())
        except Exception:
            self.web_color = None

        if colornames:
            self.template_vars['color_names'] = True
            self.template_vars['cancel'] = self.color
            self.get_css_color_names()
        elif hirespick:
            self.template_vars['hires'] = True
            self.template_vars['cancel'] = self.color
            self.template_vars['hires_color'] = hirespick
            self.get_hires_color_channel(hirespick)
        else:
            self.template_vars['picker'] = True
            self.template_vars['cancel'] = 'cancel'
            if self.hex_map:
                self.get_color_map_hex()
            else:
                self.get_color_map_square()
            self.get_current_color()
            if hsl:
                self.get_channel('channel_1', 'H', -15, 15, 'hue')
                self.get_channel('channel_2', 'S', 0.975, 1.025, 'saturation')
                self.get_channel('channel_3', 'L', 0.975, 1.025, 'luminance')
            else:
                self.get_channel('channel_1', 'R', 0.975, 1.025, 'red')
                self.get_channel('channel_2', 'G', 0.975, 1.025, 'green')
                self.get_channel('channel_3', 'B', 0.975, 1.025, 'blue')
            self.get_channel('channel_alpha', 'A', 0.975, 1.025, 'alpha')

            self.template_vars['color_switch'] = 'rgb' if self.hsl else 'hsl'
            self.get_color_info()

        mdpopups.show_popup(
            self.view,
            sublime.load_resource(
                'Packages/ColorHelper/panels/color-picker.html'),
            css=util.ADD_CSS,
            wrapper_class="color-helper content",
            max_width=1024,
            max_height=(500 if hirespick or colornames else 725),
            on_navigate=self.handle_href,
            template_vars=self.template_vars,
            nl2br=False)
示例#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 run(
        self, edit, color='#ffffff', allowed_colors=util.ALL, use_hex_argb=None,
        compress_hex=False, hsl=False, hirespick=None, colornames=False,
        on_done=None, on_cancel=None
    ):
        """Run command."""

        self.on_done = on_done
        self.on_cancel = on_cancel
        self.use_hex_argb = use_hex_argb
        self.compress_hex = compress_hex
        self.allowed_colors = allowed_colors
        self.hex_map = sublime.load_settings('color_helper.sublime-settings').get('use_hex_color_picker', True)
        rgba = util.RGBA(color)
        self.set_sizes()
        self.hsl = hsl
        self.color = rgba.get_rgba()
        self.alpha = util.fmt_float(float(int(self.color[-2:], 16)) / 255.0, 3)
        try:
            self.web_color = csscolors.hex2name(rgba.get_rgb())
        except Exception:
            self.web_color = None

        text = []
        if colornames:
            text.append('[cancel](%s){: .color-helper .small} ' % self.color)
            text.append('\n\n## CSS Color Names\n\n')
            self.get_css_color_names(text)
        elif hirespick:
            text.append('[cancel](%s){: .color-helper .small} ' % self.color)
            text.append('\n\n## %s\n\n' % hirespick)
            self.get_hires_color_channel(text, hirespick)
        else:
            text.append('[cancel](cancel){: .color-helper .small} ')
            text.append('[CSS color names](colornames){: .color-helper .small} ')
            text.append('[enter new color](edit){: .color-helper .small}\n\n')
            if self.hex_map:
                self.get_color_map_hex(text)
            else:
                self.get_color_map_square(text)
            self.get_current_color(text)
            text.append('\n\n---\n\n')
            if hsl:
                self.get_channel(text, 'H', -15, 15, 'hue')
                self.get_channel(text, 'S', 0.975, 1.025, 'saturation')
                self.get_channel(text, 'L', 0.975, 1.025, 'luminance')
            else:
                self.get_channel(text, 'R', 0.975, 1.025, 'red')
                self.get_channel(text, 'G', 0.975, 1.025, 'green')
                self.get_channel(text, 'B', 0.975, 1.025, 'blue')
            self.get_channel(text, 'A', 0.975, 1.025, 'alpha')
            text.append(
                '[switch to %s](%s){: .color-helper .small}\n' % (
                    'rgb' if self.hsl else 'hsl', 'rgb' if self.hsl else 'hsl'
                )
            )
            text.append('\n\n---\n\n')
            self.get_color_info(text)

        md = mdpopups.md2html(self.view, ''.join(text))
        mdpopups.show_popup(
            self.view, '<div class="color-helper content">%s</div>' % md,
            css=util.ADD_CSS,
            max_width=600, max_height=(500 if hirespick or colornames else 725),
            on_navigate=self.handle_href
        )