puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

Theming in Angular Material #47

Open puddlejumper26 opened 4 years ago

puddlejumper26 commented 4 years ago

Guideline

What is a theme?

A theme is a composition of color palettes (not just a single color palette, multiple color palettes) to be applied to the Angular Material components.

Primary, Warn, and Accent

In particular, a theme consists of:

Color Palette

Color Palette is a full range of colors that can be displayed on a device.[1]

👉 : Here is a resource for color palette

Inside Material design, it refers to the range of colors that canbe used within the application.

To generate a color palette, we can use Angular Material’s mat-palette mix-in. mat-palette takes a base palette (yes, that’s another palette, more on that in a second), and returns a new palette that comes with Material specific hue color values for “light”, “dark” and “contrast” colors of the given base palette. [3]

The base palette is just another color palette that compromises primary and accent colors of a single color.Let’s take Material Design’s red color palette as an example:

The values 50 - 900 represent the hue values or the “strength” of the color, or how light or dark it is.

Hue can typically be represented quantitatively by a single number, often corresponding to an angular position around a central or neutral point or axis on a colorspace coordinate diagram (such as a chromaticity diagram) or color wheel, or by its dominant wavelength or that of its complementary color [6]

take the following code as example

@function mat-palette($base-palette, $default: 500, $lighter: 100, $darker: 700) {
  $result: map_merge($base-palette, (
    default: map-get($base-palette, $default),
    lighter: map-get($base-palette, $lighter),
    darker: map-get($base-palette, $darker),

    default-contrast: mat-contrast($base-palette, $default),
    lighter-contrast: mat-contrast($base-palette, $lighter),
    darker-contrast: mat-contrast($base-palette, $darker)
  ));

  // For each hue in the palette, add a "-contrast" color to the map.
  @each $hue, $color in $base-palette {
    $result: map_merge($result, (
      '#{$hue}-contrast': mat-contrast($base-palette, $hue)
    ));
  }

  @return $result;
}

A mix-in is just a function - it takes arguments and returns something. mat-palette takes a base color palette (which is a map like $mat-red) and optional default values for the generated color palette’s default, lighter and darker colors. Eventually it returns a new color palette that has some additional map values. Those additional values are the mentioned default, lighter and darker colors, as well as their corresponding default-contrast, lighter-contrast and darker-contrast colors. On top of that it generates keys for contrast values for each base hue tone (50 - 900). [3]

👉 : IF we only need to use one color for the primary

Therefore we could use only ONE base color and its CONTRAST color to compose the simplest color palatte, for the mat-palette function of $admin-app-primary

e.g.

$sample-map: (
    500 : #000000,             <=== notice here 500 could be any number
    contrast: (                    
        500 : #FFFFFF,          <=== as long as it is identical with number here  
    )
);

/*Apllication*/
@include mat-core();
$admin-app-primary: mat-palette($sample-map);

Defining a custom theme

// define 3 theme color
/* mat-palette accepts $palette-name, main, lighter and darker variants*/
$my-theme-primary: mat-palette($mat-indigo, 700, 300, 900);
$my-theme-accent: mat-palette($mat-light-blue);
$my-theme-warn: mat-palette($mat-deep-orange, A200);

/* create theme (use mat-dark-theme for themes with dark backgrounds)*/
$my-theme: mat-light-theme(
    $my-theme-primary,
    $my-theme-accent,
    $my-theme-warn
);

The numbers behind the palette variable name select particular shades of chosen color for default, lighter and darker variants. If no numbers are provided Angular Material selects sensible defaults $default: 500, $lighter: 100, $darker: 700. [5]

Reference

[1] Customize Angular Material Design [2] Theming your Angular Material app [3] https://blog.thoughtram.io/angular/2017/05/23/custom-themes-with-angular-material.html [4] https://material.io/design/color/#color-theme-creation [5] https://medium.com/@tomastrajan/the-complete-guide-to-angular-material-themes-4d165a9d24d1 [6] https://en.wikipedia.org/wiki/Hue

puddlejumper26 commented 4 years ago

https://medium.com/razroo/customize-angular-material-design-9df5c3524c3e