# core/component/softshade (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/component/softshade/

Soft shade. (SoftShade and SoftShadeShape in vanilla.js core/component/softshade.js)
- Used to darken the area behind text so it stays readable even on a bright background. Bakes the shape into one small image and draws it stretched to the node's content size.
- It is a soft gradient, so baking at low resolution and stretching leaves no visible artifacts. (one baked pixel covers one bake unit, and there is a width and height limit)
- Rebakes when the node size, shape, color, density or side fade changes. If a bake of the same look already exists, it is shared. (sharedbakecache.rs) The node's opacity applies to it as one sheet.
- The original getBakeCanvas is get_bake_image, which returns the baked Image instead of the baked canvas.

## SoftShade (struct) (Default, Component)

Soft shade component. (bakes the shape into one small image and draws it stretched to the node's content_size)
- Initially Band shape, black, opacity 0.5 at the densest point, side fade 0, bake unit 4, width and height limit 256.
- Used to darken the area behind text so it stays readable even on a bright background. The node's opacity applies to the baked sheet as one.

Example: A dark band laid behind text

```rust
use vanilla::{Color, NodeTree, SoftShade, SoftShadeShape, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let shade_id = node_tree.create_child(root_id, "Shade");
node_tree[shade_id].set_content_size(Vector2::create(400.0, 80.0));
let soft_shade = node_tree.add_component(shade_id, SoftShade::new()).unwrap();
soft_shade.set_shade(SoftShadeShape::Band, Color::black(), 0.6, 60.0);

let soft_shade = node_tree[shade_id].get_component::<SoftShade>().unwrap();
assert_eq!(soft_shade.get_shape(), SoftShadeShape::Band);
assert_eq!(soft_shade.get_fade_width(), 60.0);
assert!(soft_shade.get_bake_image().is_none());
```

- `pub fn new() -> SoftShade` (constructor, since 0.4.0): Constructor.
  - returns SoftShade: New component with Band shape, black, opacity 0.5, side fade 0, bake unit 4, width and height limit 256.
- `pub fn draw_shade(&mut self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.4.0): Draw.
  - The bake size is content_size divided by the bake unit and rounded up, fitted to 2 ~ the width and height limit. The side fade ratio is fade_width / width (up to 0.5).
  - Rebakes if the size, shape, color, opacity or side fade differs from last time. (if a bake of the same look already exists, it is shared)
  - Does not draw if the width or height is 0 or less, or the opacity is 0 or less.
  - `transform` &NodeTransform: Node transform to draw the shade on. (reads content_size)
  - `graphic` &mut Graphic: Graphic to draw to. (with the node transform applied)
- `pub fn bake_shade(&self, bake_width: u32, bake_height: u32, fade_ratio: f64) -> Rc<Image>` (method, since 0.4.0): Bake. (computes alpha per pixel, a new image on every bake because the texture cache uploads each image once)
  - Every pixel's color is the shade color (its alpha is not used), and the alpha is 255 x opacity x vertical density x horizontal density.
  - Band: vertically 1 at the middle row, smoothly to 0 at the top and bottom ends, horizontally smoothly from 0 to 1 from both ends inward to fade_ratio. Bottom: vertically linear from 0 at the top end to 1 at the bottom end, horizontally 1.
  - `bake_width` u32: Width of the image to bake. (pixels)
  - `bake_height` u32: Height of the image to bake. (pixels)
  - `fade_ratio` f64: Width over which the sides of a Band fade, as a ratio of the image width. (0 ~ 0.5, 0 does not fade the sides, not used by Bottom)
  - returns Rc<Image>: The new baked image. (bake_width x bake_height)

  Example: Bake a shade that darkens downward at 1 x 2

  ```rust
  use vanilla::{Color, SoftShade, SoftShadeShape};
  
  let mut soft_shade = SoftShade::new();
  soft_shade.set_shade(SoftShadeShape::Bottom, Color::black(), 1.0, 0.0);
  let image = soft_shade.bake_shade(1, 2, 0.0);
  assert_eq!(image.get_width(), 1);
  assert_eq!(image.get_height(), 2);
  let pixels = image.get_pixels();
  assert_eq!(pixels[3], 64);
  assert_eq!(pixels[7], 191);
  ```

- `pub fn set_shade(&mut self, shape: SoftShadeShape, color: impl Into<Color>, opacity: f64, fade_width: f64)` (method, since 0.4.0): Sets the shade. (shape, color, opacity at the darkest point, width over which the band's sides fade)
  - `shape` SoftShadeShape: Shade shape. (initially Band)
  - `color` impl Into<Color>: Shade color. (a Color or a string such as "#rrggbb", alpha is not used, initially black)
  - `opacity` f64: Opacity at the densest point. (0 ~ 1, does not draw if 0 or less, initially 0.5)
  - `fade_width` f64: Width over which the sides of a Band fade. (node coordinates, applies only up to half the node width, initially 0)
- `pub fn set_bake_resolution(&mut self, bake_unit: f64, bake_maximum_size: u32)` (method, since 0.4.0): Sets the bake resolution. (unit covered by one baked pixel, width/height cap)
  - `bake_unit` f64: Node coordinate size covered by one baked pixel. (1 if less than 1, initially 4)
  - `bake_maximum_size` u32: Width and height limit of the baked image. (pixels, 2 if less than 2, initially 256)

  Example: Bake more coarsely, and the lower limit

  ```rust
  use vanilla::SoftShade;
  
  let mut soft_shade = SoftShade::new();
  soft_shade.set_bake_resolution(8.0, 128);
  assert_eq!(soft_shade.get_bake_unit(), 8.0);
  assert_eq!(soft_shade.get_bake_maximum_size(), 128);
  
  soft_shade.set_bake_resolution(0.5, 1);
  assert_eq!(soft_shade.get_bake_unit(), 1.0);
  assert_eq!(soft_shade.get_bake_maximum_size(), 2);
  ```

- `pub fn get_shape(&self) -> SoftShadeShape` (method, since 0.4.0): Returns the shape.
  - returns SoftShadeShape: Shade shape.
- `pub fn get_color(&self) -> Color` (method, since 0.4.0): Returns the color.
  - returns Color: Shade color.
- `pub fn get_opacity(&self) -> f64` (method, since 0.4.0): Returns the opacity at the darkest point.
  - returns f64: Opacity at the densest point. (0 ~ 1)
- `pub fn get_fade_width(&self) -> f64` (method, since 0.4.0): Returns the width over which the band's sides fade.
  - returns f64: Width over which the sides of the band fade. (node coordinates)
- `pub fn get_bake_unit(&self) -> f64` (method, since 0.4.0): Returns the unit covered by one baked pixel.
  - returns f64: Node coordinate size covered by one baked pixel. (1 or more)
- `pub fn get_bake_maximum_size(&self) -> u32` (method, since 0.4.0): Returns the width and height limit of the baked image.
  - returns u32: Width and height limit of the baked image. (pixels, 2 or more)
- `pub fn get_bake_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): Returns the baked image. (None if not baked yet, the original getBakeCanvas)
  - returns Option<&Rc<Image>>: The last baked image. None if it has never been drawn.

## SoftShadeShape (enum) (Clone, Copy, PartialEq, Eq, Debug)

Shape of the soft shade.

- `Band`: A band densest at the middle row that gradually fades toward the top and bottom ends. (the sides fade gradually inward to fade_width)
- `Bottom`: Vertical gradient darkest at the bottom edge and 0 at the top edge.
