Skip to content

Commit

Permalink
Tech review: document font-palette animation and palette-mix() (mdn#3…
Browse files Browse the repository at this point in the history
…2379)

* Add font-palette animation example

* Add palette-mix() reference page with example

* Update files/en-us/web/css/font-palette/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Update files/en-us/web/css/font-palette/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Update files/en-us/web/css/font-palette/palette-mix/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Update files/en-us/web/css/font-palette/palette-mix/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Update files/en-us/web/css/font-palette/palette-mix/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Update files/en-us/web/css/font-palette/palette-mix/index.md

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>

* Fixes for estelle review comments

* Some more small fixes after latest round of reviews

---------

Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
  • Loading branch information
chrisdavidmills and estelle authored Feb 29, 2024
1 parent 7157692 commit f22871b
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 1 deletion.
66 changes: 65 additions & 1 deletion files/en-us/web/css/font-palette/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ browser-compat: css.properties.font-palette

{{CSSRef}}

The **`font-palette`** [CSS](/en-US/docs/Web/CSS) property allows specifying one of the many palettes contained in a font that a user agent should use for the font. Users can also override the values in a palette or create a new palette by using the [@font-palette-values](/en-US/docs/Web/CSS/@font-palette-values) at-rule.
The **`font-palette`** [CSS](/en-US/docs/Web/CSS) property allows specifying one of the many palettes contained in a [color font](https://www.colorfonts.wtf/) that a user agent may use for the font. Users can also override the values in a palette or create a new palette by using the [`@font-palette-values`](/en-US/docs/Web/CSS/@font-palette-values) at-rule.

> **Note:** A `font-palette` palette takes precedence when coloring a font. The {{cssxref("color")}} property will not override a font palette, even if specified with {{cssxref("important", "!important")}}.
## Syntax

Expand All @@ -17,6 +19,9 @@ font-palette: normal;

/* Using a user-defined palette */
font-palette: --one;

/* Creating a new palette by blending two others */
font-palette: palette-mix(in lch, --blue, --yellow);
```

### Values
Expand All @@ -29,6 +34,8 @@ font-palette: --one;
- : Specifies the first palette in the font that matches 'dark' to be used for the font. Some fonts contain metadata that identify a palette as applicable for a dark (close to black) background. If a font does not have this metadata, the value behaves as `normal`.
- `<palette-identifier>`
- : Allows you to specify your own values for the font palette by using the [@font-palette-values](/en-US/docs/Web/CSS/@font-palette-values) at-rule. This value is specified using the [&lt;dashed-ident&gt;](/en-US/docs/Web/CSS/dashed-ident) format.
- {{cssxref("font-palette/palette-mix", "palette-mix()")}}
- : Creates a new `font-palette` value by blending together two `font-palette` values by specified percentages and color interpolation methods.

## Formal definition

Expand All @@ -52,6 +59,62 @@ This example allows you to use the first palette marked as _dark_ (works best on
}
```

### Animating between two palettes

This example illustrates how to animate `font-palette` value changes to create a smooth font animation.

#### HTML

The HTML contains a single paragraph of text to animate:

```html
<p>color-palette<br />animation</p>
```

#### CSS

In the CSS, we import a [color font](https://www.colorfonts.wtf/) called [Nabla](https://nabla.typearture.com/) from [Google Fonts](https://fonts.google.com/?coloronly=true), and define two custom `font-palette` values using the {{cssxref("@font-palette-values")}} at-rule. We then create {{cssxref("@keyframes")}} that animate between these two palettes, and apply this animation to our paragraph.

```css
@import url("https://fonts.googleapis.com/css2?family=Nabla&display=swap");

@font-palette-values --blueNabla {
font-family: Nabla;
base-palette: 2; /* this is Nabla's blue palette */
}

@font-palette-values --yellowNabla {
font-family: Nabla;
base-palette: 7; /* this is Nabla's yellow palette */
}

@keyframes animate-palette {
from {
font-palette: --yellowNabla;
}

to {
font-palette: --blueNabla;
}
}

p {
font-family: "Nabla";
font-size: 5rem;
margin: 0;
text-align: center;
animation: animate-palette 2s infinite alternate linear;
}
```

#### Result

The output looks like this:

{{EmbedLiveSample("Animating between two palettes", "100%", 300)}}

> **Note:** Browsers that still implement `discrete` `font-palette` animation will flip between the two palettes rather than smoothly animating.
## Specifications

{{Specifications}}
Expand All @@ -62,6 +125,7 @@ This example allows you to use the first palette marked as _dark_ (works best on

## See also

- {{cssxref("font-palette/palette-mix","palette-mix()")}}
- {{cssxref("@font-palette-values", "@font-palette-values")}}
- {{cssxref("@font-palette-values/base-palette", "base-palette")}} descriptor
- {{cssxref("@font-palette-values/font-family", "font-family")}} descriptor
Expand Down
126 changes: 126 additions & 0 deletions files/en-us/web/css/font-palette/palette-mix/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
---
title: palette-mix()
slug: Web/CSS/font-palette/palette-mix
page-type: css-function
browser-compat: css.properties.font-palette.palette-mix_function
---

{{CSSRef}}

The **`palette-mix()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) can be used to create a new {{cssxref("font-palette")}} value by blending together two `font-palette` values by specified percentages and color interpolation methods.

## Syntax

```css
/* Blending font-defined palettes */
font-palette: palette-mix(in lch, normal, dark)

/* Blending author-defined palettes */
font-palette: palette-mix(in lch, --blues, --yellows)

/* Varying percentage of each palette mixed */
font-palette: palette-mix(in lch, --blues 50%, --yellows 50%)
font-palette: palette-mix(in lch, --blues 70%, --yellows 30%)

/* Varying color interpolation method */
font-palette: palette-mix(in srgb, --blues, --yellows)
font-palette: palette-mix(in hsl, --blues, --yellows)
font-palette: palette-mix(in hsl shorter hue, --blues, --yellows)

```

### Values

Functional notation:

```text
palette-mix(method, palette1 [p1], palette2 [p2])
```

- `method`
- : A {{cssxref("&lt;color-interpolation-method&gt;")}} specifying the interpolation color space.
- `palette1`, `palette2`
- : The {{cssxref("font-palette")}} values to blend together. These can be _any_ `font-palette` values, including `palette-mix()` functions, `normal`, `dark`, and `light`.
- `p1`, `p2` {{optional_inline}}

- : {{cssxref("&lt;percentage&gt;")}} values between `0%` and `100%` specifying the amount of each palette to mix. They are normalized as follows:

- If both `p1` and `p2` are omitted, then `p1 = p2 = 50%`.
- If `p1` is omitted, then `p1 = 100% - p2`.
- If `p2` is omitted, then `p2 = 100% - p1`.
- If `p1 = p2 = 0%`, the function is invalid.
- If `p1 + p2 ≠ 100%`, then `p1' = p1 / (p1 + p2)` and `p2' = p2 / (p1 + p2)`, where `p1'` and `p2'` are the normalization results.

## Examples

### Using `palette-mix()` to blend two palettes

This example shows how to use the `palette-mix()` function to create a new palette by blending two others.

#### HTML

The HTML contains three paragraphs to apply our font information to:

```html
<p class="yellowPalette">Yellow palette</p>
<p class="bluePalette">Blue palette</p>
<p class="mixedPalette">Mixed palette</p>
```

#### CSS

In the CSS, we import a color font from Google Fonts, and define two custom `font-palette` values using the {{cssxref("@font-palette-values")}} at-rule. We then apply three different `font-palette` values to the paragraphs — `--yellow`, `--blue`, and a new green palette, created using `palette-mix()` to blend the blue and yellow palettes together.

```css
@import url("https://fonts.googleapis.com/css2?family=Nabla&display=swap");

@font-palette-values --blueNabla {
font-family: Nabla;
base-palette: 2; /* this is Nabla's blue palette */
}

@font-palette-values --yellowNabla {
font-family: Nabla;
base-palette: 7; /* this is Nabla's yellow palette */
}

p {
font-family: "Nabla";
font-size: 4rem;
text-align: center;
margin: 0;
}

.yellowPalette {
font-palette: --yellowNabla;
}

.bluePalette {
font-palette: --blueNabla;
}

.mixedPalette {
font-palette: palette-mix(in lch, --blueNabla 55%, --yellowNabla 45%);
}
```

#### Result

The output looks like this:

{{EmbedLiveSample("Using `palette-mix()` to blend two palettes", "100%", 350)}}

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{cssxref("font-palette")}}
- {{cssxref("@font-palette-values", "@font-palette-values")}}
- {{cssxref("color_value/color-mix", "color-mix()")}}
- [Color spaces and color models](/en-US/docs/Web/CSS/CSS_colors/Applying_color#color_spaces_and_color_models) for information about color spaces

0 comments on commit f22871b

Please sign in to comment.