-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
129 lines (104 loc) · 3.21 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package chessImager
import "image"
//
// ImageContext is used for advanced chess images
// (advanced images are images that includes a FEN
// string and optionally highlighted squares,
// annotations and/or moves.
//
type ImageContext struct {
pieces map[chessPiece]image.Image
pieceMap map[string]chessPiece
embeddedPieces []PieceRectangle
Fen string
Highlight []HighlightedSquare
Moves []Move
Annotations []Annotation
}
// AddHighlight adds a new highlighted square.
func (c *ImageContext) AddHighlight(square string) *ImageContext {
c.Highlight = append(c.Highlight, HighlightedSquare{Square: square})
return c
}
// AddHighlightWithStyle adds a new highlighted square with a specific style.
func (c *ImageContext) AddHighlightWithStyle(square string, style *HighlightStyle) *ImageContext {
c.Highlight = append(c.Highlight, HighlightedSquare{Square: square, Style: style})
return c
}
// AddAnnotation adds a new annotation.
func (c *ImageContext) AddAnnotation(square, text string) *ImageContext {
c.Annotations = append(c.Annotations, Annotation{Square: square, Text: text})
return c
}
// AddAnnotationWithStyle adds a new annotation with a specific style.
func (c *ImageContext) AddAnnotationWithStyle(square, text string, style *AnnotationStyle) *ImageContext {
c.Annotations = append(c.Annotations, Annotation{Square: square, Text: text, Style: style})
return c
}
// AddMove adds a move.
func (c *ImageContext) AddMove(from, to string) *ImageContext {
c.Moves = append(c.Moves, Move{From: from, To: to})
return c
}
// AddMoveWithStyle adds a move with a specific style.
func (c *ImageContext) AddMoveWithStyle(from, to string, style *MoveStyle) *ImageContext {
c.Moves = append(c.Moves, Move{From: from, To: to, Style: style})
return c
}
// NewHighlightStyle creates a new highlight style.
func (c *ImageContext) NewHighlightStyle(typ HighlightType, color string, width int, factor float64) (*HighlightStyle, error) {
col, err := hexToRGBA(color)
if err != nil {
return nil, err
}
return &HighlightStyle{
Type: typ,
Color: ColorRGBA{col},
Width: width,
Factor: factor,
}, nil
}
// NewAnnotationStyle creates a new annotation style.
func (c *ImageContext) NewAnnotationStyle(pos PositionType, size, fontSize, borderWidth int, bgc, fc,
bc string) (*AnnotationStyle, error) {
fCol, err := hexToRGBA(fc)
if err != nil {
return nil, err
}
bgCol, err := hexToRGBA(bgc)
if err != nil {
return nil, err
}
bCol, err := hexToRGBA(bc)
if err != nil {
return nil, err
}
return &AnnotationStyle{
Position: pos,
Size: size,
FontColor: ColorRGBA{fCol},
FontSize: fontSize,
BackgroundColor: ColorRGBA{bgCol},
BorderColor: ColorRGBA{bCol},
BorderWidth: borderWidth,
}, nil
}
// NewMoveStyle creates a new move style.
func (c *ImageContext) NewMoveStyle(typ MoveType, color string, color2 string, factor float64,
padding float64) (*MoveStyle, error) {
col, err := hexToRGBA(color)
if err != nil {
return nil, err
}
col2, err := hexToRGBA(color2)
if err != nil {
return nil, err
}
return &MoveStyle{
Color: ColorRGBA{col},
Color2: ColorRGBA{col2},
Type: typ,
Factor: factor,
Padding: padding,
}, nil
}