Skip to content

Instantly share code, notes, and snippets.

@jfcherng
Last active December 7, 2023 07:27
Show Gist options
  • Select an option

  • Save jfcherng/3e2d2acadc91962ded6379ed2884aa7f to your computer and use it in GitHub Desktop.

Select an option

Save jfcherng/3e2d2acadc91962ded6379ed2884aa7f to your computer and use it in GitHub Desktop.

Revisions

  1. jfcherng revised this gist Oct 21, 2023. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions srgb_norm.py
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,13 @@ def handle_coord(val: float):
    return int(val) if val.is_integer() else val

    coords = parent.clone().coords(nans=False)
    return "{r} {g} {b}".format(
    r=handle_coord(coords[0]),
    g=handle_coord(coords[1]),
    b=handle_coord(coords[2]),
    return "{r} {g} {b}".format_map(
    dict(
    zip(
    ("r", "g", "b"),
    map(handle_coord, coords),
    )
    )
    )


  2. jfcherng revised this gist Oct 21, 2023. 2 changed files with 6 additions and 10 deletions.
    6 changes: 3 additions & 3 deletions color_helper.sublime-settings
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,9 @@
    "class": "ColorHelper.custom.srgb_norm.ColorSrgbNorm",
    "filters": ["srgb"],
    "output": [
    {"space": "srgb", "format": {"precision": 6}}
    ]
    }
    {"space": "srgb", "format": {"precision": 6}},
    ],
    },
    },
    "user_color_rules": [
    {
    10 changes: 3 additions & 7 deletions srgb_norm.py
    Original file line number Diff line number Diff line change
    @@ -23,13 +23,9 @@ def match(cls, string: str, start: int = 0, fullmatch: bool = True):
    m = MATCH.match(string, start)
    if not m or (fullmatch and m.end(0) != len(string)):
    return None
    return (
    (
    (float(m.group("r")), float(m.group("g")), float(m.group("b"))),
    1.0, # alpha
    ),
    m.end(0),
    )

    r, g, b, a = map(float, m.groups()), 1.0
    return ((r, g, b), a), m.end(0)

    @classmethod
    def to_string(cls, parent, *, precision=6):
  3. jfcherng revised this gist Oct 21, 2023. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions srgb_norm.py
    Original file line number Diff line number Diff line change
    @@ -34,11 +34,16 @@ def match(cls, string: str, start: int = 0, fullmatch: bool = True):
    @classmethod
    def to_string(cls, parent, *, precision=6):
    """Output color in `R G B` form."""

    def handle_coord(val: float):
    val = round(val, precision)
    return int(val) if val.is_integer() else val

    coords = parent.clone().coords(nans=False)
    return "{r} {g} {b}".format(
    r=round(coords[0], precision),
    g=round(coords[1], precision),
    b=round(coords[2], precision),
    r=handle_coord(coords[0]),
    g=handle_coord(coords[1]),
    b=handle_coord(coords[2]),
    )


  4. jfcherng revised this gist Oct 20, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion color_helper.sublime-settings
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    },
    "user_color_rules": [
    {
    "name": "XML",
    "name": "XML", // overwrites the built-in "XML" color rule
    "base_scopes": ["text.xml"],
    "color_class": "srgb_norm",
    "scanning": [
  5. jfcherng revised this gist Oct 20, 2023. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,7 @@
    Original request: https://discord.com/channels/280102180189634562/280157083356233728/1164923362829676626
    ## Original Request

    https://discord.com/channels/280102180189634562/280157083356233728/1164923362829676626

    ## Screenshot

    ![image](https://user-images.githubusercontent.com/6594915/277018291-94f82aec-1841-44a8-b788-6d48ddce2146.png)
  6. jfcherng created this gist Oct 20, 2023.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Original request: https://discord.com/channels/280102180189634562/280157083356233728/1164923362829676626
    23 changes: 23 additions & 0 deletions color_helper.sublime-settings
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // file: Packages/User/color_helper.sublime-settings
    {
    "user_color_classes": {
    "srgb_norm": {
    "class": "ColorHelper.custom.srgb_norm.ColorSrgbNorm",
    "filters": ["srgb"],
    "output": [
    {"space": "srgb", "format": {"precision": 6}}
    ]
    }
    },
    "user_color_rules": [
    {
    "name": "XML",
    "base_scopes": ["text.xml"],
    "color_class": "srgb_norm",
    "scanning": [
    "text.xml meta.tag",
    ],
    "color_trigger": "(?<=value=\")(?=[\\d.]+\\s+[\\d.]+\\s+[\\d.]+)",
    },
    ],
    }
    49 changes: 49 additions & 0 deletions srgb_norm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # file: Packages/ColorHelper/custom/srgb_norm.py

    import re

    from ColorHelper.ch_util import get_base_color

    from ..lib.coloraide.spaces.srgb.css import sRGB

    MATCH = re.compile(
    r"""
    (?P<r>\d+(?:\.\d+)?)\s+
    (?P<g>\d+(?:\.\d+)?)\s+
    (?P<b>\d+(?:\.\d+)?)
    """,
    re.VERBOSE,
    )


    class SrgbNorm(sRGB):
    @classmethod
    def match(cls, string: str, start: int = 0, fullmatch: bool = True):
    """Match and parse a color string."""
    m = MATCH.match(string, start)
    if not m or (fullmatch and m.end(0) != len(string)):
    return None
    return (
    (
    (float(m.group("r")), float(m.group("g")), float(m.group("b"))),
    1.0, # alpha
    ),
    m.end(0),
    )

    @classmethod
    def to_string(cls, parent, *, precision=6):
    """Output color in `R G B` form."""
    coords = parent.clone().coords(nans=False)
    return "{r} {g} {b}".format(
    r=round(coords[0], precision),
    g=round(coords[1], precision),
    b=round(coords[2], precision),
    )


    class ColorSrgbNorm(get_base_color()):
    pass


    ColorSrgbNorm.register(SrgbNorm(), overwrite=True)