Discord Components & Role Selection (#5)
This commit is contained in:
34
core/color.go
Normal file
34
core/color.go
Normal file
@ -0,0 +1,34 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IntToColor converts a hex int to a Go Color
|
||||
func IntToColor(hex int) color.Color {
|
||||
r := uint8(hex >> 16 & 0xFF)
|
||||
g := uint8(hex >> 8 & 0xFF)
|
||||
b := uint8(hex & 0xFF)
|
||||
return color.RGBA{r, g, b, 255}
|
||||
}
|
||||
|
||||
// ColorToInt converts a Go Color to a hex int
|
||||
func ColorToInt(c color.Color) int {
|
||||
rgba := color.RGBAModel.Convert(c).(color.RGBA)
|
||||
hex := int(rgba.R)<<16 | int(rgba.G)<<8 | int(rgba.B)
|
||||
return hex
|
||||
}
|
||||
|
||||
// HexToColor coverts hex string to color
|
||||
func HexToColor(s string) (color.Color, error) {
|
||||
s = strings.ReplaceAll(s, "#", "")
|
||||
|
||||
hex, err := strconv.ParseInt(s, 16, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return IntToColor(int(hex)), nil
|
||||
|
||||
}
|
75
core/color_test.go
Normal file
75
core/color_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntToColor(t *testing.T) {
|
||||
|
||||
// green
|
||||
hex := 0x00FF00
|
||||
expected := color.RGBA{0, 255, 0, 255}
|
||||
got := IntToColor(hex)
|
||||
require.Equal(t, expected, got)
|
||||
|
||||
// black
|
||||
hex = 0x000000
|
||||
expected = color.RGBA{0, 0, 0, 255}
|
||||
got = IntToColor(hex)
|
||||
require.Equal(t, expected, got)
|
||||
|
||||
// white
|
||||
hex = 0xFFFFFF
|
||||
expected = color.RGBA{255, 255, 255, 255}
|
||||
got = IntToColor(hex)
|
||||
require.Equal(t, expected, got)
|
||||
|
||||
}
|
||||
|
||||
func TestColorToHex(t *testing.T) {
|
||||
|
||||
// magenta
|
||||
col := color.RGBA{255, 0, 255, 255}
|
||||
hex := 0xFF00FF
|
||||
require.Equal(t, hex, ColorToInt(col))
|
||||
|
||||
// black
|
||||
col = color.RGBA{0, 0, 0, 255}
|
||||
hex = 0x000000
|
||||
require.Equal(t, hex, ColorToInt(col))
|
||||
|
||||
// white
|
||||
col = color.RGBA{255, 255, 255, 255}
|
||||
hex = 0xFFFFFF
|
||||
require.Equal(t, hex, ColorToInt(col))
|
||||
}
|
||||
|
||||
func TestHexToColor(t *testing.T) {
|
||||
|
||||
// magenta
|
||||
hex := "#ff00ff"
|
||||
col := color.RGBA{255, 0, 255, 255}
|
||||
|
||||
c, err := HexToColor(hex)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, col, c)
|
||||
|
||||
// black
|
||||
hex = "000000"
|
||||
col = color.RGBA{0, 0, 0, 255}
|
||||
|
||||
c, err = HexToColor(hex)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, col, c)
|
||||
|
||||
// white
|
||||
hex = "ffffff"
|
||||
col = color.RGBA{255, 255, 255, 255}
|
||||
|
||||
c, err = HexToColor(hex)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, col, c)
|
||||
}
|
@ -17,6 +17,21 @@ type DiscordConfig struct {
|
||||
EventCategory string `yaml:"event_category" env:"DISCORD_EVENT_CATEGORY"`
|
||||
ArchiveCategory string `yaml:"archive_category" env:"DISCORD_ARCHIVE_CATEGORY"`
|
||||
NotificationChannel string `yaml:"notification_channel" env:"DISCORD_NOTIFICATION_CHANNEL"`
|
||||
|
||||
RoleSelections []RoleSelectionConfig `yaml:"role_selection"`
|
||||
}
|
||||
|
||||
type RoleSelectionConfig struct {
|
||||
Title string `yaml:"title"`
|
||||
Description string `yaml:"description"`
|
||||
|
||||
SelectionChannel string `yaml:"discord_channel"`
|
||||
Roles []RoleConfig `yaml:"roles"`
|
||||
}
|
||||
|
||||
type RoleConfig struct {
|
||||
RoleName string `yaml:"name"`
|
||||
Color string `yaml:"color"`
|
||||
}
|
||||
|
||||
// MastodonConfig contains mastodon specific configuration
|
||||
@ -33,6 +48,7 @@ type Features struct {
|
||||
ManageEventChannels Feature `yaml:"manage_event_channels" env:"BIRD_EVENT_CHANNELS"`
|
||||
AnnounceEvents Feature `yaml:"announce_events" env:"BIRD_ANNOUNCE_EVENTS"`
|
||||
ReccurringEvents Feature `yaml:"recurring_events" env:"BIRD_RECURRING_EVENTS"`
|
||||
RoleSelection Feature `yaml:"role_selection" env:"BIRD_ROLE_SELECTION"`
|
||||
LoadGamePlugins Feature `yaml:"load_game_plugins" env:"BIRD_LOAD_GAME_PLUGINS"`
|
||||
}
|
||||
|
||||
|
@ -4,3 +4,8 @@ package core
|
||||
func Bool(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
// Int returns a pointer to an int
|
||||
func Int(v int) *int {
|
||||
return &v
|
||||
}
|
||||
|
Reference in New Issue
Block a user