Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Button Background #55

Open
Fire-Cube opened this issue Oct 17, 2022 · 4 comments
Open

Change Button Background #55

Fire-Cube opened this issue Oct 17, 2022 · 4 comments

Comments

@Fire-Cube
Copy link

Hello rdbende

I want to be able to change the color of a button. Usually by hex code.
Since the buttons are pictures, I don't know how to do that.

Do you have any idea?

@rdbende
Copy link
Owner

rdbende commented Oct 17, 2022

Hi! Thank you for using my theme!

Currently setting a custom color isn't supported by this theme, though I plan to implement it somehow. In the meanwhile you can find some hacking in #12.

@Fire-Cube
Copy link
Author

Thank you for the quick reply. I will try this additional script tomorrow.

@Fire-Cube
Copy link
Author

Fire-Cube commented Oct 18, 2022

Hello rdbende. I tried the code and cleaned it up. The change_widget_color function can now be used to change the color of a single image.

from pathlib import Path
from PIL import Image
from PIL.ImageColor import getcolor, getrgb
from PIL.ImageOps import grayscale

THEME_PATH = "./light/"

def image_tint(src, tint="#ffffff"):
    src = Image.open(src)
    if src.mode not in ["RGB", "RGBA"]:
        raise TypeError(f"Unsupported source image mode: {src.mode}")

    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")  # tint color's overall luminosity
    if not tl:
        tl = 1  # avoid division by zero

    tl = float(tl)  # compute luminosity preserving tint factors
    sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb))  # per component
    luts = (
        tuple(map(lambda lr: int(lr * sr + 0.5), range(256)))
        + tuple(map(lambda lg: int(lg * sg + 0.5), range(256)))
        + tuple(map(lambda lb: int(lb * sb + 0.5), range(256)))
    )
    l = grayscale(src)  # 8-bit luminosity version of whole image
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))  # for RGB verion of grayscale

    else:  # include copy of src image's alpha layer
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))
        merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
        luts += tuple(range(256))  # for 1:1 mapping of copied alpha values

    return Image.merge(*merge_args).point(luts)

def make_transparent(src):
    img = Image.open(src)
    img = img.convert("RGBA")
    datas = img.getdata()

    newData = []
    for item in datas:
        if item[0] == 224 and item[1] == 232 and item[2] == 243:
            newData.append((255, 255, 255, 0))

        else:
            newData.append(item)

    img.putdata(newData)
    img.save(src, "PNG")

def process(input_path, output_path, color):
    result = image_tint(input_path, color)
    result.save(output_path)

def change_widget_color(name, color):
    path = Path(THEME_PATH, name)
    make_transparent(path)
    process(path, path, color)

change_widget_color("arrow-right.png", "#654321")

@Fire-Cube
Copy link
Author

What is the best way to customize this for a single widget?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants