Module: TableTennis::Util::Scale
- Includes:
- MemoWise
- Defined in:
- lib/table_tennis/util/scale.rb
Overview
Helpers for color scales (adding background colors to a column based on the value of the float in the cell). Like conditional formattiong with a color range in google sheets.
Constant Summary collapse
- SCALES =
{ # white => color g: [WHITE, GREEN], y: [WHITE, YELLOW], r: [WHITE, RED], b: [WHITE, BLUE], # green/yellow/red => white gw: [GREEN, WHITE], yw: [YELLOW, WHITE], rw: [RED, WHITE], bw: [BLUE, WHITE], # red <=> green rg: [RED, WHITE, GREEN], gr: [GREEN, WHITE, RED], gyr: [GREEN, YELLOW, RED], }
Class Method Summary collapse
Class Method Details
.color_stops(name) ⇒ Object
47 48 49 |
# File 'lib/table_tennis/util/scale.rb', line 47 def color_stops(name) SCALES[name].map { Colors.to_rgb(_1) }.transpose end |
.interpolate(name, t) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/table_tennis/util/scale.rb', line 32 def interpolate(name, t) t = t.clamp(0, 1).to_f stops = color_stops(name) rgb = if stops.first.length == 2 stops.map { lerp(t, _1, _2) } elsif t < 0.5 t *= 2 stops.map { lerp(t, _1, _2) } else t = (t - 0.5) * 2 stops.map { lerp(t, _2, _3) } end Colors.to_hex(rgb) end |
.lerp(t, v0, v1) ⇒ Object
52 |
# File 'lib/table_tennis/util/scale.rb', line 52 def lerp(t, v0, v1) = t * (v1 - v0) + v0 |