Skip to content

Commit

Permalink
feat: add color fade function and RBG (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
floaust committed Oct 11, 2020
1 parent d40b002 commit ec35c9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/map_range_to_range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package internal

func MapRangeToRange(fromMin, fromMax, toMin, toMax, current float32) int {
return int(toMin + ((toMax-toMin)/(fromMax-fromMin))*(current-fromMin))
}
13 changes: 13 additions & 0 deletions internal/map_range_to_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package internal

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMapRangeToRange(t *testing.T) {
assert.Equal(t, 127, MapRangeToRange(0, 100, 0, 255, 50))
assert.Equal(t, 127, MapRangeToRange(0, 400, 0, 255, 200))
assert.Equal(t, 127, MapRangeToRange(-200, 200, 0, 255, 0))
assert.Equal(t, 127, MapRangeToRange(0, 200.123, 0, 254.3, 100))
}
26 changes: 26 additions & 0 deletions rgb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pterm

import "github.com/pterm/pterm/internal"

// RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.
// The name of the model comes from the initials of the three additive primary colors, red, green, and blue.
// https://en.wikipedia.org/wiki/RGB_color_model
type RGB struct {
R uint8
G uint8
B uint8
}

// NewRGB returns a new RGB.
func NewRGB(r, g, b uint8) RGB {
return RGB{R: r, G: g, B: b}
}

// Fade fades one RGB value to another RGB value, by giving the function a minimum, maximum and current value.
func (rgb RGB) Fade(min, max, current float32, end RGB) RGB {
return RGB{
R: uint8(internal.MapRangeToRange(min, max, float32(rgb.R), float32(end.R), current)),
G: uint8(internal.MapRangeToRange(min, max, float32(rgb.G), float32(end.G), current)),
B: uint8(internal.MapRangeToRange(min, max, float32(rgb.B), float32(end.B), current)),
}
}

0 comments on commit ec35c9d

Please sign in to comment.