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

Feature: Allow color-gradient to replace original colors #390

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions filters/color-gradient/src/ColorGradientFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type DefaultOptions = {
angle?: number;
alpha?: number;
maxColors?: number;
replace?: boolean;
};

export type CssOptions = {
Expand Down Expand Up @@ -59,6 +60,7 @@ class ColorGradientFilter extends Filter
alpha: 1.0,
angle: 90.0,
maxColors: 0,
replace: false,
};

private _stops: ColorStop[] = [];
Expand Down Expand Up @@ -182,6 +184,21 @@ class ColorGradientFilter extends Filter
{
return this.uniforms.uMaxColors;
}

/**
* If true, the gradient will replace the existing color, otherwise it
* will be multiplied with it
* @default false
*/
set replace(value: boolean)
{
this.uniforms.uReplace = value;
}

get replace(): boolean
{
return this.uniforms.uReplace;
}
}

export { ColorGradientFilter };
Expand Down
10 changes: 8 additions & 2 deletions filters/color-gradient/src/colorGradient.frag
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ uniform int uType;
uniform float uAngle;
uniform float uAlpha;
uniform int uMaxColors;
uniform bool uReplace;

struct ColorStop {
float offset;
Expand Down Expand Up @@ -122,6 +123,11 @@ void main(void) {
float gradientAlpha = uAlpha * currentColor.a;
vec4 gradientColor = mix(colorFrom, colorTo, relativePercent) * gradientAlpha;

// mix resulting color with current color
gl_FragColor = gradientColor + currentColor*(1.-gradientColor.a);
if (uReplace == false) {
// mix resulting color with current color
gl_FragColor = gradientColor + currentColor*(1.-gradientColor.a);
} else {
// replace with gradient color
gl_FragColor = gradientColor;
}
}
1 change: 1 addition & 0 deletions tools/demo/src/filters/color-gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default function ()
folder.add(this, 'alpha', 0, 1);
folder.add(this, 'angle', 0, 360, 1);
folder.add(this, 'maxColors', 0, 24, 1);
folder.add(this, 'replace', false);
applyDefaultOptions(stops);
},
});
Expand Down