# ReactionEquation

> Reaction holds a reaction's formulas. atom_counts reads one, with parentheses (Ca(OH)2). balance finds the smallest whole coefficients.

Page: https://fframes-kit.pages.dev/docs/chemistry/reaction-equation/
Cargo feature: `chemistry` (`fframes-kit = { version = "..", features = ["chemistry"] }`)

`Reaction` holds a reaction's formulas. `atom_counts` reads one, with parentheses (`Ca(OH)2`).
`balance` finds the smallest whole coefficients. It returns `None` when the reaction cannot be
balanced or has more than one balance. `tally` counts each element on both sides.

`ReactionEquation` needs the `latex` feature as well. It turns the equation without
coefficients into the balanced one: the species slide apart and the coefficients fade in,
starting at `delay` and taking `run_time` seconds. Typeset both with `Reaction::typeset` in
the constructor. With `tally` a line under the equation counts the atoms of each element on
both sides. It shows `unequal_color` until the balance and `equal_color` after it. Gallery
scene: `ReactionEquation` (propane burning).

Gallery clip of `ReactionEquation`: https://fframes-kit.pages.dev/media/scenes/ReactionEquation-673aab06.mp4

```rust
use fframes_kit::prelude::*;

let reaction = Reaction::new(&["C3H8", "O2"], &["CO2", "H2O"]);
assert_eq!(reaction.balance(), Some(vec![1, 5, 3, 4]));
assert_eq!(reaction.ce(Some(&[1, 5, 3, 4])), "C3H8 + 5O2 -> 3CO2 + 4H2O");

fn figure<'a>(
    ui: &mut Ui<'a, '_, '_>,
    reaction: &'a Reaction,
    unbalanced: &'a Formula,
    balanced: &'a Formula,
) -> Svgr<'a> {
    // unbalanced = reaction.typeset(&engine, false)?, balanced = reaction.typeset(&engine, true)?
    ui.show(ReactionEquation {
        reaction: Some(reaction),
        unbalanced: Some(unbalanced),
        balanced: Some(balanced),
        delay: 1.0,
        ..ui.defaults()
    })
}
```

## API

Components are plain structs. Set the fields you need and fill the rest with `..ui.defaults()`: the defaults below come from the current theme (`theme.` values are fields of `Theme`).

### ReactionEquation

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `reaction` | `Option<&Reaction>` | `None` | The `Reaction` the atom tally counts and balances; `None` for no tally. |
| `unbalanced` | `Option<&Formula>` | `None` | The typeset equation before balancing (from `Reaction::typeset` with `false`); `None` shows only `balanced`. |
| `balanced` | `Option<&Formula>` | `None` | The typeset equation after balancing (from `Reaction::typeset` with `true`); `None` shows only `unbalanced`. |
| `center` | `(f64, f64)` | `(0.0, 0.0)` | The middle of the equation, in units of `ui.space`. |
| `size` | `f32` | `110.0` | Size of the equation: pixels for a 10 pt font, the tally sits `0.9 * size` pixels under the middle. |
| `delay` | `f32` | `0.0` | Seconds after the container's start before the equation turns into its balanced form. |
| `run_time` | `f32` | `1.2` | Seconds the turn into the balanced equation takes; the tally switches halfway through. |
| `tally` | `bool` | `true` | Writes a line counting each element's atoms on both sides (`H  4 \| 4`); `false` for none. |
| `color` | `Color` | `theme.colors.text` | Colour of the equation. |
| `equal_color` | `Color` | `Color::hex("#69db7c")` | Colour of an element's tally when both sides have the same count. |
| `unequal_color` | `Color` | `Color::hex("#ff6b6b")` | Colour of an element's tally when the two sides differ. |
| `parts_enter` | `Option<FadeIn>` | `None` | Fade in of the tally; `None` for none. |
| `parts_exit` | `Option<FadeOut>` | `None` | Fade out of the tally before the end; `None` for none. |

Feature `chemistry` · [Source](https://github.com/khanhthanhdev/fframes-kit/blob/main/fframes-kit/src/kits/chemistry/reaction.rs#L259)
