Debian Codegraph @

writer/standard/image_option.go

raw
1 package standard
2
3 import (
4 "fmt"
5 "image"
6 "image/color"
7
8 "github.com/yeqown/go-qrcode/v2"
9 )
10
11 type ImageOption interface {
12 apply(o *outputImageOptions)
13 }
14
15 // defaultOutputImageOption default output image background color and etc options
16 func defaultOutputImageOption() *outputImageOptions {
17 return &outputImageOptions{
18 bgColor: color_WHITE, // white
19 bgTransparent: false, // not transparent
20 qrColor: color_BLACK, // black
21 logo: nil, //
22 logoSizeMultiplier: 5,
23 qrWidth: 20, //
24 shape: _shapeRectangle, //
25 imageEncoder: jpegEncoder{},
26 borderWidths: [4]int{_defaultPadding, _defaultPadding, _defaultPadding, _defaultPadding},
27 }
28 }
29
30 // outputImageOptions to output QR code image
31 type outputImageOptions struct {
32 // bgColor is the background color of the QR code image.
33 bgColor color.RGBA
34 // bgTransparent only affects on PNG_FORMAT
35 bgTransparent bool
36
37 // qrColor is the foreground color of the QR code.
38 qrColor color.RGBA
39
40 // logo this icon image would be put the center of QR Code image
41 // NOTE: logo only should have 1 / logoSizeMultiplier size of QRCode image
42 logo image.Image
43
44 logoSizeMultiplier int
45
46 // qrWidth width of each qr block
47 qrWidth int
48
49 // shape means how to draw the shape of each cell.
50 shape IShape
51
52 // imageEncoder specify which file format would be encoded the QR image.
53 imageEncoder ImageEncoder
54
55 // borderWidths indicates the border width of the output image. the order is
56 // top, right, bottom, left same as the WithBorder
57 borderWidths [4]int
58
59 // halftoneImg is the halftone image for the output image.
60 halftoneImg image.Image
61 }
62
63 func (oo *outputImageOptions) backgroundColor() color.RGBA {
64 if oo == nil {
65 return color_WHITE
66 }
67
68 if oo.bgTransparent {
69 (&oo.bgColor).A = 0x00
70 }
71
72 return oo.bgColor
73 }
74
75 func (oo *outputImageOptions) logoImage() image.Image {
76 if oo == nil || oo.logo == nil {
77 return nil
78 }
79
80 return oo.logo
81 }
82
83 func (oo *outputImageOptions) qrBlockWidth() int {
84 if oo == nil || (oo.qrWidth <= 0 || oo.qrWidth > 255) {
85 return 20
86 }
87
88 return oo.qrWidth
89 }
90
91 func (oo *outputImageOptions) getShape() IShape {
92 if oo == nil || oo.shape == nil {
93 return _shapeRectangle
94 }
95
96 return oo.shape
97 }
98
99 // preCalculateAttribute this function must reference to draw function.
100 func (oo *outputImageOptions) preCalculateAttribute(dimension int) *Attribute {
101 if oo == nil {
102 return nil
103 }
104
105 top, right, bottom, left := oo.borderWidths[0], oo.borderWidths[1], oo.borderWidths[2], oo.borderWidths[3]
106 return &Attribute{
107 W: dimension*oo.qrBlockWidth() + right + left,
108 H: dimension*oo.qrBlockWidth() + top + bottom,
109 Borders: oo.borderWidths,
110 BlockWidth: oo.qrBlockWidth(),
111 }
112 }
113
114 var (
115 color_WHITE = parseFromHex("#ffffff")
116 color_BLACK = parseFromHex("#000000")
117 )
118
119 var (
120 // _STATE_MAPPING mapping matrix.State to color.RGBA in debug mode.
121 _STATE_MAPPING = map[qrcode.QRType]color.RGBA{
122 qrcode.QRType_INIT: parseFromHex("#ffffff"), // [bg]
123 qrcode.QRType_DATA: parseFromHex("#cdc9c3"), // [bg]
124 qrcode.QRType_VERSION: parseFromHex("#000000"), // [fg]
125 qrcode.QRType_FORMAT: parseFromHex("#444444"), // [fg]
126 qrcode.QRType_FINDER: parseFromHex("#555555"), // [fg]
127 qrcode.QRType_DARK: parseFromHex("#2BA859"), // [fg]
128 qrcode.QRType_SPLITTER: parseFromHex("#2BA859"), // [fg]
129 qrcode.QRType_TIMING: parseFromHex("#000000"), // [fg]
130 }
131 )
132
133 // translateToRGBA get color.RGBA by value State, if not found, return outputImageOptions.qrColor.
134 // NOTE: this function decides the state should use qrColor or bgColor.
135 func (oo *outputImageOptions) translateToRGBA(v qrcode.QRValue) (rgba color.RGBA) {
136 // TODO(@yeqown): use _STATE_MAPPING to replace this function while in debug mode
137 // or some special flag.
138 if v.IsSet() {
139 rgba = oo.qrColor
140 return rgba
141 }
142
143 if oo.bgTransparent {
144 (&oo.bgColor).A = 0x00
145 // color.RGBA is pre-multiplied by alpha, so set RGB to 0 when fully transparent.
146 (&oo.bgColor).R = 0x00
147 (&oo.bgColor).G = 0x00
148 (&oo.bgColor).B = 0x00
149 }
150 rgba = oo.bgColor
151
152 return rgba
153 }
154
155 // parseFromHex convert hex string into color.RGBA
156 func parseFromHex(s string) color.RGBA {
157 c := color.RGBA{
158 R: 0,
159 G: 0,
160 B: 0,
161 A: 0xff,
162 }
163
164 var err error
165 switch len(s) {
166 case 7:
167 _, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
168 case 4:
169 _, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B)
170 // Double the hex digits:
171 c.R *= 17
172 c.G *= 17
173 c.B *= 17
174 default:
175 err = fmt.Errorf("invalid length, must be 7 or 4")
176 }
177 if err != nil {
178 panic(err)
179 }
180
181 return c
182 }
183
184 func parseFromColor(c color.Color) color.RGBA {
185 rgba, ok := c.(color.RGBA)
186 if ok {
187 return rgba
188 }
189
190 r, g, b, a := c.RGBA()
191 return color.RGBA{
192 R: uint8(r),
193 G: uint8(g),
194 B: uint8(b),
195 A: uint8(a),
196 }
197 }