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

Alpha channel is not implemented for colours #114

Open
willmcenaney opened this issue Dec 14, 2020 · 2 comments
Open

Alpha channel is not implemented for colours #114

willmcenaney opened this issue Dec 14, 2020 · 2 comments

Comments

@willmcenaney
Copy link

Alpha compatibility & functions like fade are not implemented.

It looks like we'd need to convert the entire colour library to rgba to implement fade.

@natevw
Copy link

natevw commented Sep 22, 2021

It looks like we'd need to convert the entire colour library to rgba to implement fade.

The color library isn't easy to follow, but afaict each method is ± independent of the others. At least talking of the methods intended to be called as LESS helper functions.

Thus, while it would be great if the whole set could get updated to handle things more consistently, to support chaining and whatnot, as it sits I was able to implement a fade without much issue:

def fade(self, color, diff, *args):
    """ Set opacity of color.
    args:
        color (str): color
        diff (str): percentage
    returns:
        str
    """
    
    if color and diff:
        if isinstance(diff, string_types):
            diff = float(diff.strip('%'))
        rgb = self._hextorgb(color)
        rgba = tuple(rgb) + (diff / 100.0,)
        return "rgba(%s,%s,%s, %0.3f)" % rgba
    
    raise ValueError('Illegal color values')

The "cheat" is that fade doesn't care about the existing alpha anyway and so it's fine that that information gets lost. At least, if the call to fade() is the last thing in a chain — you won't have reliable luck passing the result of this back to any further functions.

(It surprised me at first to find that I could pass in an rgb(…) color to this method since — like all the existing ones that go through _ophsl — it only expects hex formatted strings. Of course the CSS syntax for this looks like a method call to this LESS parser so the string has already been run through Color.rgb before it gets run through this Color.fade.)

@natevw
Copy link

natevw commented Sep 22, 2021

It looks like this project is still somewhat maintained, so lmk if you'd like a pull request with the above.

For my own use recently I ended up being able to use mix(@mycolor, white, 42%) to get a pre-blended color that didn't actually depend on the content behind it but I know that I've missed this basic alpha support other times here.

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