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

Color alpha support #19

Open
coderofsalvation opened this issue Nov 22, 2021 · 7 comments
Open

Color alpha support #19

coderofsalvation opened this issue Nov 22, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@coderofsalvation
Copy link

coderofsalvation commented Nov 22, 2021

Is this somehow supported?
If not: how about just adding it by just using a rgb colorpocker, and entering the 'AA' value manually in the textbox (thats how dat.gui solved it).

@georgealways
Copy link
Owner

No, at the moment alpha values are going to get chopped off the end of a hex string. It would be nice if the GUI "ignored" the last two characters and passed them along like you describe, I'll consider adding that.

Adding a separate alpha slider for an Object or Array based color is pretty straightforward, but it's admittedly annoying for an #rrggbbaa string:

let color;

const obj = { rgb: '#aabbcc', alpha: 1 };

gui.addColor( obj, 'rgb' ).onChange( update );
gui.add( obj, 'alpha', 0, 1 ).onChange( update );

update();

function update() { 
  color = obj.rgb + Math.round( obj.alpha * 0xff ).toString( 16 ).padStart( 2, 0 ); 
};

Hope that gets you by for now. I'd be curious to hear more about your use case too.

@georgealways georgealways added the enhancement New feature or request label Nov 22, 2021
@awelles
Copy link

awelles commented Nov 22, 2021

Here's a snippet to preserve #rrggbbaa format on an object using an intermediate object:

const params = { color: '#ff0000aa' };  // object you ultimately want to play with

const obj = { color: params.color.slice( 0, -2 ) };  // intermediate object

gui.addColor( obj, 'color' )
    .onChange( value => {
        params.color = value + params.color.slice( -2 );
    } );

Note: A downside here is that the controller is attached to obj and so won't detect changes to params.color with .listen().

@georgealways georgealways changed the title #RRGGBBAA anyone? ColorController ignore/preserve alpha for #RRGGBBAA strings Nov 22, 2021
@amatyulkov
Copy link

I'd be excited to see rgba() or #rrggbbaa support in this project. I'm using lil-gui to provide demos of a web component that I'm building. The component uses a gradient to render loading indicators, and the default config I have for the gradient has semi-transparent colors written as rgba(128,128,128,0.2).

I can get away with just using the same colors without the alpha channel, but if I wanted to, for example, put one semi-transparent element over another for added visual effect, I wouldn't be able to do it comfortably with lil-gui. I would still be able to control opacity through additional controllers, but that's a lot more code than I'd like to write for a demo.

The demo is here: https://amatyulkov.github.io/legendary-skeleton/examples/01-basic.html.

@georgealways
Copy link
Owner

hey @amatyulkov thanks for chiming in (and for using an existing issue 😉)

So the main reason we don't have alpha support is because color controllers use the native input[type=color] element. The built in color picker spares us from a lot of complexity (color space conversions, mobile support) ... but it doesn't support alpha.

I'd be curious to see a pass at an "advanced" color controller (written from scratch in lil-gui's visual style) that does everything the native color picker does + alpha. It would probably make the library a fair deal bigger, so maybe that could live as some kind of an "extension".

Gonna make this the general color alpha thread for anyone else who has this same request, or thoughts on how to implement.

@georgealways georgealways changed the title ColorController ignore/preserve alpha for #RRGGBBAA strings Color alpha support May 25, 2022
@amatyulkov
Copy link

I've done some more lurking. Dat.gui's color controller is 300 LOC without the alpha support. I think 300 LOC would make lil-gui not-so-lil, and ColorWithAlpha is better as a controller extension.

What's your take on this?

@georgealways
Copy link
Owner

Agreed—longer reply in #12. And keep in mind that 300 lines doesn't include any of the color math or CSS 😉

@nuthinking
Copy link

+1 on adding Alpha support

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

No branches or pull requests

5 participants