Exemple #1
0
    def setup_image_border(self):
        """Setup_image_border."""

        ch_settings = sublime.load_settings('color_helper.sublime-settings')
        border_color = ch_settings.get('image_border_color')
        if border_color is not None:
            try:
                border_color = Color(border_color, filters=util.SRGB_SPACES)
                border_color.fit("srgb", in_place=True)
            except Exception:
                border_color = None

        if border_color is None:
            # Calculate border color for images
            border_color = Color(self.view.style()['background'],
                                 filters=util.SRGB_SPACES).convert("hsl")
            border_color.lightness = border_color.lightness + (
                30 if border_color.luminance() < 0.5 else -30)

        self.default_border = border_color.convert("srgb").to_string(**HEX)
        self.out_of_gamut = Color("transparent",
                                  filters=util.SRGB_SPACES).to_string(**HEX)
        self.out_of_gamut_border = Color(
            self.view.style().get('redish', "red"),
            filters=util.SRGB_SPACES).to_string(**HEX)
Exemple #2
0
    def get_html_style(self):
        """Get HTML style."""

        styles = self.view.style()
        fg = styles['foreground']
        bg = styles['background']
        temp = Color(bg).convert("srgb")
        is_dark = temp.luminance() < 0.5
        bg = temp.mix("white" if is_dark else "black", 0.05).to_string(**util.HEX)
        code = temp.mix("white" if is_dark else "black", 0.15).to_string(**util.HEX)
        font = sublime.load_settings("Preferences.sublime-settings").get('font_face', 'Courier')
        return STYLE.format(fg=fg, bg=bg, code=code, font=font)
Exemple #3
0
    def do_search(self, force=False):
        """
        Perform the search for the highlighted word.

        TODO: This function is a big boy. We should look into breaking it up.
              With that said, this is low priority.
        """

        # Since the plugin has been reloaded, force update.
        global reload_flag
        settings = self.view.settings()
        colors = []

        # Allow per view scan override
        option = settings.get("color_helper.scan_override", None)
        if option in ("Force enable", "Force disable"):
            override = option == "Force enable"
        else:
            override = None

        # Get the rules and use them to get the needed scopes.
        # The scopes will be used to get the searchable regions.
        rules = util.get_rules(self.view)
        # Bail if this if this view has no valid rule or scanning is disabled.
        if (rules is None or not rules.get("enabled", False)
                or (not rules.get("allow_scanning", True) and not override)
                or override is False):
            self.erase_phantoms()
            return

        if reload_flag:
            reload_flag = False
            force = True

        # Calculate size of preview boxes
        box_height = self.calculate_box_size()
        check_size = int((box_height - 2) / 4)
        if check_size < 2:
            check_size = 2

        # If desired preview boxes are different than current,
        # we need to reload the boxes.
        old_box_height = int(settings.get('color_helper.box_height', 0))
        current_color_scheme = settings.get('color_scheme')
        if (force or old_box_height != box_height
                or current_color_scheme != settings.get(
                    'color_helper.color_scheme', '')
                or settings.get('color_helper.refresh')):
            self.erase_phantoms()
            settings.set('color_helper.color_scheme', current_color_scheme)
            settings.set('color_helper.box_height', box_height)
            force = True

        # If we don't need to force previews,
        # quit if visible region is the same as last time
        visible_region = self.view.visible_region()
        if not force and self.previous_region == visible_region:
            return
        self.previous_region = visible_region

        # Setup "preview on select"
        preview_on_select = ch_settings.get("preview_on_select", False)
        show_preview = True
        sel = None
        if preview_on_select and len(self.view.sel()) != 1:
            show_preview = False
        elif preview_on_select:
            sel = self.view.sel()[0]

        # Get the scan scopes
        scanning = rules.get("scanning")
        classes = rules.get("color_class", "css-level-4")
        if show_preview and visible_region.size() and scanning and classes:
            # Get out of gamut related options
            self.setup_gamut_options()

            # Get triggers that identify where colors are likely
            color_trigger = re.compile(
                rules.get("color_trigger", util.RE_COLOR_START))

            # Find source content in the visible region.
            # We will return consecutive content, but if the lines are too wide
            # horizontally, they will be clipped and returned as separate chunks.
            for src_region in self.source_iter(visible_region):
                source = self.view.substr(src_region)
                start = 0

                # Find colors in this source chunk.
                for m in color_trigger.finditer(source):
                    # Test if we have found a valid color
                    start = m.start()
                    src_start = src_region.begin() + start

                    # Check if the first point within the color matches our scope rules
                    # and load up the appropriate color class
                    color_class, filters = self.get_color_class(
                        src_start, classes)
                    if color_class is None:
                        continue

                    # Check if scope matches for scanning
                    try:
                        value = self.view.score_selector(src_start, scanning)
                        if not value:
                            continue
                    except Exception:
                        continue

                    obj = color_class.match(source,
                                            start=start,
                                            filters=filters)
                    if obj is not None:
                        # Calculate true start and end of the color source
                        src_end = src_region.begin() + obj.end
                        region = sublime.Region(src_start, src_end)

                        # If "preview on select" is enabled, only show preview if within a selection
                        # or if the selection as no width and the color comes right after.
                        if (preview_on_select
                                and not (sel.empty()
                                         and sel.begin() == region.begin())
                                and not region.intersects(sel)):
                            continue
                    else:
                        continue

                    # Calculate point at which we which to insert preview
                    position_on_left = preview_is_on_left()
                    pt = src_start if position_on_left else src_end
                    if str(region.begin()) in self.previews:
                        # Already exists
                        continue

                    # Calculate a reasonable border color for our image at this location and get color strings
                    hsl = Color(mdpopups.scope2style(
                        self.view, self.view.scope_name(pt))['background'],
                                filters=util.SRGB_SPACES).convert("hsl")
                    hsl.lightness = hsl.lightness + (
                        30 if hsl.luminance() < 0.5 else -30)
                    preview_border = hsl.convert(
                        "srgb", fit=True).to_string(**util.HEX)

                    color = Color(obj.color)
                    title = ''
                    if not color.in_gamut("srgb"):
                        title = ' title="Out of gamut"'
                        if self.show_out_of_gamut_preview:
                            srgb = color.convert("srgb", fit=True)
                            preview1 = srgb.to_string(**util.HEX_NA)
                            preview2 = srgb.to_string(**util.HEX)
                        else:
                            preview1 = self.out_of_gamut
                            preview2 = self.out_of_gamut
                            preview_border = self.out_of_gamut_border
                    else:
                        srgb = color.convert("srgb")
                        preview1 = srgb.to_string(**util.HEX_NA)
                        preview2 = srgb.to_string(**util.HEX)

                    # Create preview
                    unique_id = str(time()) + str(region)
                    html = PREVIEW_IMG.format(
                        unique_id, title,
                        mdpopups.color_box([preview1, preview2],
                                           preview_border,
                                           height=box_height,
                                           width=box_height,
                                           border_size=PREVIEW_BORDER_SIZE,
                                           check_size=check_size))
                    colors.append(
                        (html, pt, region.begin(), region.end(), unique_id))

            # Add all previews
            self.add_phantoms(colors)

            # The phantoms may have altered the viewable region,
            # so set previous region to the current viewable region
            self.previous_region = sublime.Region(
                self.previous_region.begin(),
                self.view.visible_region().end())