-
Notifications
You must be signed in to change notification settings - Fork 1
/
composer.go
125 lines (99 loc) · 2.83 KB
/
composer.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
package mosaic
import (
"github.com/fogleman/gg"
"image"
"sort"
)
// A Composer creates image compositions
type Composer interface {
// Compose draws the images to the drawing context.
Compose(dc *gg.Context, images ...image.Image) error
}
// A ComposerFunc is a Composer which itself is a function.
type ComposerFunc func(dc *gg.Context, images ...image.Image) error
// Compose calls the underlying function with the given arguments.
func (f ComposerFunc) Compose(dc *gg.Context, images ...image.Image) error {
return f(dc, images...)
}
// ComposerInfo is a Composer with additional information.
type ComposerInfo struct {
Composer
Id string
Name string
Description string
ImageCountHuman string
CheckImageCount func(count int) bool
RecommendedImageCounts []int
}
// RecommendImageCount recommends a suitable amount of images to use
// which is guaranteed to be less or equal to the amount provided.
func (ci ComposerInfo) RecommendImageCount(imageCount int) int {
var currentBest int
for _, c := range ci.RecommendedImageCounts {
if c > currentBest && c <= imageCount {
currentBest = c
}
}
// found a recommended count
if currentBest > 0 {
return currentBest
}
// no check provided, assume all values are possible
if ci.CheckImageCount == nil {
return imageCount
}
// check all counts and take the first that works
for i := imageCount; i > 0; i-- {
if ci.CheckImageCount(i) {
return i
}
}
return 0
}
var registeredComposers []ComposerInfo
// RegisterComposer registers the given
func RegisterComposer(comps ...ComposerInfo) error {
for _, c := range comps {
registeredComposers = append(registeredComposers, c)
}
return nil
}
// GetComposer returns the composer with the given id.
func GetComposer(id string) (ComposerInfo, bool) {
for _, composer := range registeredComposers {
if composer.Id == id {
return composer, true
}
}
return ComposerInfo{}, false
}
// GetComposers returns a slice containing all composers.
func GetComposers() []ComposerInfo {
return registeredComposers
}
// RecommendComposers returns a slice of composers which are suitable
// for the given image count.
func RecommendComposers(count int) []ComposerInfo {
type ComposerComp struct {
C ComposerInfo
RecImageCount int
}
composerComparisons := make([]ComposerComp, 0)
for _, composer := range registeredComposers {
recommended := composer.RecommendImageCount(count)
if recommended != 0 {
composerComparisons = append(composerComparisons, ComposerComp{
composer,
recommended,
})
}
}
sort.Slice(composerComparisons, func(i, j int) bool {
return composerComparisons[i].RecImageCount < composerComparisons[j].RecImageCount
})
composers := make([]ComposerInfo, len(composerComparisons))
for i, cc := range composerComparisons {
composers[i] = cc.C
}
return composers
}