2018-03-15 18:45:52 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-15 18:45:52 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-15 18:45:52 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-15 18:45:52 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-15 18:45:52 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package widget
|
2018-03-15 18:45:52 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-18 20:24:03 +01:00
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
|
2022-04-15 11:53:09 +02:00
|
|
|
"go.mau.fi/tcell"
|
2020-04-16 18:27:35 +02:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix/id"
|
2018-03-15 18:45:52 +01:00
|
|
|
)
|
|
|
|
|
2020-02-19 22:42:26 +01:00
|
|
|
var colorNames = []string{
|
|
|
|
"maroon",
|
|
|
|
"green",
|
|
|
|
"olive",
|
|
|
|
"navy",
|
|
|
|
"purple",
|
|
|
|
"teal",
|
|
|
|
"silver",
|
|
|
|
"gray",
|
|
|
|
"red",
|
|
|
|
"lime",
|
|
|
|
"yellow",
|
|
|
|
"blue",
|
|
|
|
"fuchsia",
|
|
|
|
"aqua",
|
|
|
|
"white",
|
|
|
|
"aliceblue",
|
|
|
|
"antiquewhite",
|
|
|
|
"aquamarine",
|
|
|
|
"azure",
|
|
|
|
"beige",
|
|
|
|
"bisque",
|
|
|
|
"blanchedalmond",
|
|
|
|
"blueviolet",
|
|
|
|
"brown",
|
|
|
|
"burlywood",
|
|
|
|
"cadetblue",
|
|
|
|
"chartreuse",
|
|
|
|
"chocolate",
|
|
|
|
"coral",
|
|
|
|
"cornflowerblue",
|
|
|
|
"cornsilk",
|
|
|
|
"crimson",
|
|
|
|
"darkblue",
|
|
|
|
"darkcyan",
|
|
|
|
"darkgoldenrod",
|
|
|
|
"darkgray",
|
|
|
|
"darkgreen",
|
|
|
|
"darkkhaki",
|
|
|
|
"darkmagenta",
|
|
|
|
"darkolivegreen",
|
|
|
|
"darkorange",
|
|
|
|
"darkorchid",
|
|
|
|
"darkred",
|
|
|
|
"darksalmon",
|
|
|
|
"darkseagreen",
|
|
|
|
"darkslateblue",
|
|
|
|
"darkslategray",
|
|
|
|
"darkturquoise",
|
|
|
|
"darkviolet",
|
|
|
|
"deeppink",
|
|
|
|
"deepskyblue",
|
|
|
|
"dimgray",
|
|
|
|
"dodgerblue",
|
|
|
|
"firebrick",
|
|
|
|
"floralwhite",
|
|
|
|
"forestgreen",
|
|
|
|
"gainsboro",
|
|
|
|
"ghostwhite",
|
|
|
|
"gold",
|
|
|
|
"goldenrod",
|
|
|
|
"greenyellow",
|
|
|
|
"honeydew",
|
|
|
|
"hotpink",
|
|
|
|
"indianred",
|
|
|
|
"indigo",
|
|
|
|
"ivory",
|
|
|
|
"khaki",
|
|
|
|
"lavender",
|
|
|
|
"lavenderblush",
|
|
|
|
"lawngreen",
|
|
|
|
"lemonchiffon",
|
|
|
|
"lightblue",
|
|
|
|
"lightcoral",
|
|
|
|
"lightcyan",
|
|
|
|
"lightgoldenrodyellow",
|
|
|
|
"lightgray",
|
|
|
|
"lightgreen",
|
|
|
|
"lightpink",
|
|
|
|
"lightsalmon",
|
|
|
|
"lightseagreen",
|
|
|
|
"lightskyblue",
|
|
|
|
"lightslategray",
|
|
|
|
"lightsteelblue",
|
|
|
|
"lightyellow",
|
|
|
|
"limegreen",
|
|
|
|
"linen",
|
|
|
|
"mediumaquamarine",
|
|
|
|
"mediumblue",
|
|
|
|
"mediumorchid",
|
|
|
|
"mediumpurple",
|
|
|
|
"mediumseagreen",
|
|
|
|
"mediumslateblue",
|
|
|
|
"mediumspringgreen",
|
|
|
|
"mediumturquoise",
|
|
|
|
"mediumvioletred",
|
|
|
|
"midnightblue",
|
|
|
|
"mintcream",
|
|
|
|
"mistyrose",
|
|
|
|
"moccasin",
|
|
|
|
"navajowhite",
|
|
|
|
"oldlace",
|
|
|
|
"olivedrab",
|
|
|
|
"orange",
|
|
|
|
"orangered",
|
|
|
|
"orchid",
|
|
|
|
"palegoldenrod",
|
|
|
|
"palegreen",
|
|
|
|
"paleturquoise",
|
|
|
|
"palevioletred",
|
|
|
|
"papayawhip",
|
|
|
|
"peachpuff",
|
|
|
|
"peru",
|
|
|
|
"pink",
|
|
|
|
"plum",
|
|
|
|
"powderblue",
|
|
|
|
"rebeccapurple",
|
|
|
|
"rosybrown",
|
|
|
|
"royalblue",
|
|
|
|
"saddlebrown",
|
|
|
|
"salmon",
|
|
|
|
"sandybrown",
|
|
|
|
"seagreen",
|
|
|
|
"seashell",
|
|
|
|
"sienna",
|
|
|
|
"skyblue",
|
|
|
|
"slateblue",
|
|
|
|
"slategray",
|
|
|
|
"snow",
|
|
|
|
"springgreen",
|
|
|
|
"steelblue",
|
|
|
|
"tan",
|
|
|
|
"thistle",
|
|
|
|
"tomato",
|
|
|
|
"turquoise",
|
|
|
|
"violet",
|
|
|
|
"wheat",
|
|
|
|
"whitesmoke",
|
|
|
|
"yellowgreen",
|
|
|
|
"grey",
|
|
|
|
"dimgrey",
|
|
|
|
"darkgrey",
|
|
|
|
"darkslategrey",
|
|
|
|
"lightgrey",
|
|
|
|
"lightslategrey",
|
|
|
|
"slategrey",
|
2018-03-15 18:45:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:03:36 +01:00
|
|
|
// GetHashColorName gets a color name for the given string based on its FNV-1 hash.
|
|
|
|
//
|
|
|
|
// The array of possible color names are the alphabetically ordered color
|
|
|
|
// names specified in tcell.ColorNames.
|
|
|
|
//
|
|
|
|
// The algorithm to get the color is as follows:
|
2022-11-13 14:40:10 +01:00
|
|
|
//
|
|
|
|
// colorNames[ FNV1(string) % len(colorNames) ]
|
2018-03-22 22:03:36 +01:00
|
|
|
//
|
|
|
|
// With the exception of the three special cases:
|
2022-11-13 14:40:10 +01:00
|
|
|
//
|
|
|
|
// --> = green
|
|
|
|
// <-- = red
|
|
|
|
// --- = yellow
|
2018-03-18 20:24:03 +01:00
|
|
|
func GetHashColorName(s string) string {
|
|
|
|
switch s {
|
|
|
|
case "-->":
|
|
|
|
return "green"
|
|
|
|
case "<--":
|
|
|
|
return "red"
|
|
|
|
case "---":
|
|
|
|
return "yellow"
|
|
|
|
default:
|
|
|
|
h := fnv.New32a()
|
2019-01-11 23:44:32 +01:00
|
|
|
_, _ = h.Write([]byte(s))
|
|
|
|
return colorNames[h.Sum32()%uint32(len(colorNames))]
|
2018-03-15 18:45:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-18 20:24:03 +01:00
|
|
|
|
2018-03-22 22:03:36 +01:00
|
|
|
// GetHashColor gets the tcell Color value for the given string.
|
|
|
|
//
|
|
|
|
// GetHashColor calls GetHashColorName() and gets the Color value from the tcell.ColorNames map.
|
2020-04-16 18:27:35 +02:00
|
|
|
func GetHashColor(val interface{}) tcell.Color {
|
|
|
|
switch str := val.(type) {
|
|
|
|
case string:
|
|
|
|
return tcell.ColorNames[GetHashColorName(str)]
|
|
|
|
case *string:
|
|
|
|
return tcell.ColorNames[GetHashColorName(*str)]
|
|
|
|
case id.UserID:
|
|
|
|
return tcell.ColorNames[GetHashColorName(string(str))]
|
|
|
|
default:
|
|
|
|
return tcell.ColorNames["red"]
|
|
|
|
}
|
2018-03-18 20:24:03 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:33:59 +02:00
|
|
|
// AddColor adds tview color tags to the given string.
|
|
|
|
func AddColor(s, color string) string {
|
|
|
|
return fmt.Sprintf("[%s]%s[white]", color, s)
|
2018-03-18 20:24:03 +01:00
|
|
|
}
|