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

Add alpha blending #231

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/index-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {default as inGamut} from "./inGamut.js";
export {default as toGamut} from "./toGamut.js";
export {default as distance} from "./distance.js";
export {default as equals} from "./equals.js";
export {default as over} from "./over.js";
export {default as contrast} from "./contrast.js";
export {default as clone} from "./clone.js";
export {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Color.extend(deltaE);
import * as variations from "./variations.js";
Color.extend(variations);

import over from "./over.js";
Color.extend(over);

import contrast from "./contrast.js";
Color.extend(contrast);

Expand Down
47 changes: 47 additions & 0 deletions src/over.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import getColor from "./getColor.js";
import ColorSpace from "./space.js";
import xyz_d65 from "./spaces/xyz-d65.js";
import to from "./to.js";

export default function over (source, backdrop, {
space = xyz_d65,
outputSpace = source.space,
} = {}) {
source = getColor(source);
backdrop = getColor(backdrop);

space = ColorSpace.get(space);
outputSpace = ColorSpace.get(outputSpace);

if (space.isPolar) {
throw new Error("Compositing in polar color spaces is not supported.");
}

let result;

if (source.alpha === 0) {
result = backdrop;
}
else if (source.alpha === 1 || backdrop.alpha === 0) {
result = source;
}
else {
let source_xyz = to(source, space);
let backdrop_xyz = to(backdrop, space);

result = {
space,
coords: source_xyz.map((s, i) => {
let b = backdrop_xyz[i];
return s + b * (1 - source.alpha);
}),
alpha: source.alpha + backdrop.alpha * (1 - source.alpha)
}
}

return to(result, outputSpace);
}

export function register (Color) {
Color.defineFunction("over", over, {returns: "color"});
}
12 changes: 12 additions & 0 deletions types/src/over.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Color, { ColorTypes } from "./color";

export interface OverOptions {
space?: string | ColorSpace | undefined;
outputSpace?: string | ColorSpace | undefined;
}

export function over(
source: ColorTypes,
backdrop: ColorTypes,
options?: OverOptions
) : ColorTypes