Beispiel #1
0
// StringToColor returns a tcell color from a string representation of a color
// We accept either bright... or light... to mean the brighter version of a color
func StringToColor(str string) tcell.Color {
	switch str {
	case "black":
		return tcell.ColorBlack
	case "red":
		return tcell.ColorMaroon
	case "green":
		return tcell.ColorGreen
	case "yellow":
		return tcell.ColorOlive
	case "blue":
		return tcell.ColorNavy
	case "magenta":
		return tcell.ColorPurple
	case "cyan":
		return tcell.ColorTeal
	case "white":
		return tcell.ColorSilver
	case "brightblack", "lightblack":
		return tcell.ColorGray
	case "brightred", "lightred":
		return tcell.ColorRed
	case "brightgreen", "lightgreen":
		return tcell.ColorLime
	case "brightyellow", "lightyellow":
		return tcell.ColorYellow
	case "brightblue", "lightblue":
		return tcell.ColorBlue
	case "brightmagenta", "lightmagenta":
		return tcell.ColorFuchsia
	case "brightcyan", "lightcyan":
		return tcell.ColorAqua
	case "brightwhite", "lightwhite":
		return tcell.ColorWhite
	case "default":
		return tcell.ColorDefault
	default:
		// Check if this is a 256 color
		if num, err := strconv.Atoi(str); err == nil {
			return GetColor256(num)
		}
		// Probably a truecolor hex value
		return tcell.GetColor(str)
	}
}
Beispiel #2
0
func GetSprite(name string) *Sprite {

	spriteDataL.Lock()
	data, ok := spriteData[name]
	spriteDataL.Unlock()
	if !ok {
		return nil
	}

	s := NewSprite(data.Width, data.Height)

	glyphs := make(map[byte]rune)
	styles := make(map[byte]tcell.Style)
	count := data.Width * data.Height

	for k, g := range data.Glyphs {
		st := tcell.StyleDefault
		fn := g.Foreground
		if cn, ok := data.Palette[fn]; ok {
			fn = cn
		}
		bn := g.Background
		if cn, ok := data.Palette[bn]; ok {
			bn = cn
		}

		fg := tcell.GetColor(fn)
		st = st.Foreground(fg)

		bg := tcell.GetColor(bn)
		st = st.Background(bg)

		glyphs[k[0]] = []rune(g.Display)[0]
		styles[k[0]] = st
	}

	for i, fr := range data.Frames {
		f := &spriteFrame{nextFrame: fr.Next}
		f.timer = time.Millisecond * time.Duration(fr.Time)
		f.runes = make([]rune, count)
		f.styles = make([]tcell.Style, count)
		for y, line := range fr.Data {
			for x := range line {
				c := line[x]
				i := y*data.Width + x
				if c == ' ' {
					continue
				}
				f.runes[i] = glyphs[c]
				f.styles[i] = styles[c]
			}
		}
		for _, name := range fr.Names {
			s.frames[name] = f
		}
		// Also insert a default based on index position
		name := strconv.Itoa(i)
		if _, ok := s.frames[name]; ok {
			s.frames[name] = f
		}
		s.frames[name] = f
	}
	s.SetOrigin(data.OriginX, data.OriginY)
	s.SetLayer(data.Layer)
	return s
}