-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add color fade function and
RBG
(#34)
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
} | ||
} |