Skip to content

colorAdd

Adds two colors together.

Mixes two colors by meeting halfway in HSL: the hue lands between the two along the shorter way around the wheel, and saturation and lightness are averaged. Red and blue give purple, red and yellow give orange.

import {colorAdd} from "@d3plus/color";

colorAdd("#ff0000", "#0000ff");
colorAdd("red", "yellow");
colorAdd("#1c7ed6", "#2f9e44");

The optional third and fourth arguments weight each color from 0 to 1. The weights scale the hue, saturation, and lightness terms before they are averaged, so a partial weight is not a simple fade: at o2: 0.25 red and blue mix to a dark brown, at 0.5 to an olive, and only at 1 to the full purple.

import {colorAdd} from "@d3plus/color";

colorAdd("#ff0000", "#0000ff", 1, 0.25);
colorAdd("#ff0000", "#0000ff", 1, 0.5);
colorAdd("#ff0000", "#0000ff", 1, 1);

Parameters

import {colorAdd} from "@d3plus/color";
c1requiredstring

The first color, a valid CSS color string.

c2requiredstring

The second color, also a valid CSS color string.

o1numberDefault 1

Value from 0 to 1 of the first color's opacity.

o2numberDefault 1

Value from 0 to 1 of the first color's opacity.