Exemplo n.º 1
0
    def test_lightness(self):
        """Test `lightness`."""

        c = Color('color(--okhsl 120 50% 90% / 1)')
        self.assertEqual(c.lightness, 0.9)
        c.lightness = 0.8
        self.assertEqual(c.lightness, 0.8)
Exemplo n.º 2
0
    def test_lightness(self):
        """Test `lightness`."""

        c = Color('color(--lchuv 90% 50 120 / 1)')
        self.assertEqual(c.lightness, 90)
        c.lightness = 80
        self.assertEqual(c.lightness, 80)
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def test_lightness(self):
        """Test `lightness`."""

        c = Color('color(--oklab 1 0.2 -0.3 / 1)')
        self.assertEqual(c.lightness, 1)
        c.lightness = 0.2
        self.assertEqual(c.lightness, 0.2)
Exemplo n.º 5
0
    def test_lightness(self):
        """Test `lightness`."""

        c = Color('color(--hsluv 120 50% 90% / 1)')
        self.assertEqual(c.lightness, 90)
        c.lightness = 80
        self.assertEqual(c.lightness, 80)
Exemplo n.º 6
0
    def get_color_map_square(self):
        """Get a square variant of the color map."""

        global color_map
        global color_map_size
        global line_height
        global default_border
        global color_scale
        global last_saturation

        s = self.color.convert("hsl").saturation

        # Only update if the last time we rendered we changed
        # something that would require a new render.
        if (color_map is None or s != last_saturation
                or self.graphic_size != color_map_size
                or self.graphic_scale != color_scale
                or self.line_height != line_height
                or self.default_border != default_border):
            color_map_size = self.graphic_size
            color_scale = self.graphic_scale

            line_height = self.line_height
            default_border = self.default_border

            html_colors = []

            # Generate the colors with each row being dark than the last.
            # Each column will progress through hues.
            color = Color("hsl(0 {}% 90%)".format(s), filters=util.SRGB_SPACES)
            hfac = 24.0
            lfac = 8.0
            check_size = self.check_size(self.height)
            for y in range(0, 11):
                html_colors.append([self.get_spacer(width=5)])
                for x in range(0, 15):
                    value = color.convert("srgb").to_string(**HEX)
                    kwargs = {
                        "border_size": BORDER_SIZE,
                        "height": self.height,
                        "width": self.width,
                        "check_size": check_size
                    }

                    if y == 0 and x == 0:
                        border_map = colorbox.TOP | colorbox.LEFT
                    elif y == 0 and x == 14:
                        border_map = colorbox.TOP | colorbox.RIGHT
                    elif y == 0:
                        border_map = colorbox.TOP
                    elif y == 10 and x == 0:
                        border_map = colorbox.BOTTOM | colorbox.LEFT
                    elif y == 10 and x == 14:
                        border_map = colorbox.BOTTOM | colorbox.RIGHT
                    elif y == 10:
                        border_map = colorbox.BOTTOM
                    elif x == 0:
                        border_map = colorbox.LEFT
                    elif x == 14:
                        border_map = colorbox.RIGHT
                    else:
                        border_map = 0
                    kwargs["border_map"] = border_map

                    html_colors[-1].append('<a href="{}">{}</a>'.format(
                        color.to_string(**COLOR_FULL_PREC),
                        mdpopups.color_box([value], self.default_border,
                                           **kwargs)))
                    color.hue = color.hue + hfac
                color.hue = 0.0
                color.lightness = color.lightness - lfac

            # Generate a grayscale bar.
            lfac = 10.0
            color = Color('hsl(0 0% 100%)', filters=util.SRGB_SPACES)
            check_size = self.check_size(self.height)
            for y in range(0, 11):
                value = color.convert("srgb").to_string(**HEX)
                kwargs = {
                    "border_size": BORDER_SIZE,
                    "height": self.height,
                    "width": self.width,
                    "check_size": check_size
                }

                if y == 0:
                    border_map = 0xb
                elif y == 10:
                    border_map = 0xe
                else:
                    border_map = 0xa
                kwargs["border_map"] = border_map

                html_colors[y].append('<a href="{}">{}</a>'.format(
                    color.to_string(**COLOR_FULL_PREC),
                    mdpopups.color_box([value], self.default_border,
                                       **kwargs)))
                color.lightness = color.lightness - lfac

            color_map = (''.join([
                '<span>{}</span><br>'.format(''.join([y1 for y1 in x1]))
                for x1 in html_colors
            ]) + '\n\n')
        self.template_vars['color_picker'] = color_map
Exemplo n.º 7
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())