liveBook · Manning

Color palette

As explained in Pro Angular 16

A color palette is a defined collection of colors from which interface colors are selected.

A color palette is a defined collection of colors from which interface colors are selected. Angular Material organizes palettes around named roles such as primary, accent, and warn, allowing components to use consistent colors and contrasting text. section 28.3.2 section 2.4.1

Use palettes to coordinate component appearance:

  • Select a base palette, such as the indigo, pink, or red palette, containing multiple hues for one color. section 28.3.2
  • Define named palettes for primary, accent, and warning states, optionally choosing specific accent hues. section 28.3.2
  • Retrieve both a palette’s default color and its default-contrast color so foreground text remains appropriate. section 28.3.2
  • Reuse theme background and foreground palettes for states such as disabled controls. section 28.3.2

Applying palettes to a custom button

A custom button can expose a themeColor value such as primary, accent, or warn. SCSS maps each value to a defined palette, applies its default color as the background, and applies the matching default-contrast color to the label. The same component can therefore offer several theme-consistent variants without duplicating the complete style definition.

Listing 28.19. The customButton.component.scss file in the src/app/core folder
@use "@angular/material" as material;
 
$primary: material.define-palette(material.$indigo-palette);
$accent: material.define-palette(material.$pink-palette, A200, A100, A400);
$warn: material.define-palette(material.$red-palette);
 
$typography: material.define-typography-config();
 
button[class*="custom-button-"] {
    padding: 7px 12px;
    border: none;
    border-radius: 4px;
    margin: 2px;
}
 
button.custom-button {
    @each $name, $palette in (primary: $primary, 
            accent: $accent, warn: $warn) {
        &-#{$name} {
            background-color: 
                material.get-color-from-palette($palette, default);
            color: material.get-color-from-palette($palette, 
                default-contrast);
            font: {
                family: material.font-family($typography, button);
                size: material.font-size($typography, button);
                weight: material.font-weight($typography, button);
            } 
        }
    }    
}
 
$bg: material.$light-theme-background-palette;
$fg: material.$light-theme-foreground-palette;
 
:host[disabled] button[class*="custom-button-"], 
         button[class*="custom-button-"]:disabled  {
    background-color: 
        material.get-color-from-palette($bg, disabled-button);
    color: material.get-color-from-palette($fg, disabled-button);
}

Inspect the complete SCSS implementation, including palette definitions, typography configuration, generated button variants, and disabled-state colors. section 28.3.2

How Pro Angular 16 covers it

How deep it goes
Defines base palettes and demonstrates Sass-based color and contrast retrieval, while deferring broader theme customization to documentation and package source.
Examples
Demonstrates indigo, pink, and red palettes in custom buttons and primary or accent colors on a toolbar and badge.
What it assumes
Assumes an Angular project and basic familiarity with Sass or SCSS, while introducing Angular Material palette variables and functions.
What you can do after
Build it from scratch with Angular Material Sass by defining palettes and applying generated colors to custom component states.
How it connects
Connects color palettes with Angular Material themes, Sass, SCSS, typography, and custom components.

Also explained in

Compare how 11 books explain color palette →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.