-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
158 lines (136 loc) · 4.18 KB
/
color.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2022 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package color
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/alexzh7/pdfcpu/pkg/pdfcpu/types"
"github.com/pkg/errors"
)
// Some popular colors.
var (
Black = SimpleColor{}
White = SimpleColor{R: 1, G: 1, B: 1}
LightGray = SimpleColor{.9, .9, .9}
Gray = SimpleColor{.5, .5, .5}
DarkGray = SimpleColor{.3, .3, .3}
Red = SimpleColor{1, 0, 0}
Green = SimpleColor{0, 1, 0}
Blue = SimpleColor{0, 0, 1}
)
var ErrInvalidColor = errors.New("pdfcpu: invalid color constant")
// SimpleColor is a simple rgb wrapper.
type SimpleColor struct {
R, G, B float32 // intensities between 0 and 1.
}
func (sc SimpleColor) String() string {
return fmt.Sprintf("r=%1.1f g=%1.1f b=%1.1f", sc.R, sc.G, sc.B)
}
func (sc SimpleColor) Array() types.Array {
return types.NewNumberArray(float64(sc.R), float64(sc.G), float64(sc.B))
}
// NewSimpleColor returns a SimpleColor for rgb in the form 0x00RRGGBB
func NewSimpleColor(rgb uint32) SimpleColor {
r := float32((rgb>>16)&0xFF) / 255
g := float32((rgb>>8)&0xFF) / 255
b := float32(rgb&0xFF) / 255
return SimpleColor{r, g, b}
}
// NewSimpleColorForArray returns a SimpleColor for an r,g,b array.
func NewSimpleColorForArray(arr types.Array) SimpleColor {
r := float32(arr[0].(types.Float).Value())
g := float32(arr[1].(types.Float).Value())
b := float32(arr[2].(types.Float).Value())
return SimpleColor{r, g, b}
}
// NewSimpleColorForHexCode returns a SimpleColor for a #FFFFFF type hexadecimal rgb color representation.
func NewSimpleColorForHexCode(hexCol string) (SimpleColor, error) {
var sc SimpleColor
if len(hexCol) != 7 || hexCol[0] != '#' {
return sc, errors.Errorf("pdfcpu: invalid hex color string: #FFFFFF, %s\n", hexCol)
}
b, err := hex.DecodeString(hexCol[1:])
if err != nil || len(b) != 3 {
return sc, errors.Errorf("pdfcpu: invalid hex color string: #FFFFFF, %s\n", hexCol)
}
return SimpleColor{float32(b[0]) / 255, float32(b[1]) / 255, float32(b[2]) / 255}, nil
}
func internalSimpleColor(s string) (SimpleColor, error) {
var (
sc SimpleColor
err error
)
switch strings.ToLower(s) {
case "black":
sc = Black
case "darkgray":
sc = DarkGray
case "gray":
sc = Gray
case "lightgray":
sc = LightGray
case "white":
sc = White
case "red":
sc = Red
case "green":
sc = Green
case "blue":
sc = Blue
default:
err = ErrInvalidColor
}
return sc, err
}
// ParseColor turns a color string into a SimpleColor.
func ParseColor(s string) (SimpleColor, error) {
var sc SimpleColor
cs := strings.Split(s, " ")
if len(cs) != 1 && len(cs) != 3 {
return sc, errors.Errorf("pdfcpu: illegal color string: 3 intensities 0.0 <= i <= 1.0 or #FFFFFF, %s\n", s)
}
if len(cs) == 1 {
if len(cs[0]) == 7 && cs[0][0] == '#' {
// #FFFFFF to uint32
return NewSimpleColorForHexCode(cs[0])
}
return internalSimpleColor(cs[0])
}
r, err := strconv.ParseFloat(cs[0], 32)
if err != nil {
return sc, errors.Errorf("red must be a float value: %s\n", cs[0])
}
if r < 0 || r > 1 {
return sc, errors.New("pdfcpu: red: a color value is an intensity between 0.0 and 1.0")
}
sc.R = float32(r)
g, err := strconv.ParseFloat(cs[1], 32)
if err != nil {
return sc, errors.Errorf("pdfcpu: green must be a float value: %s\n", cs[1])
}
if g < 0 || g > 1 {
return sc, errors.New("pdfcpu: green: a color value is an intensity between 0.0 and 1.0")
}
sc.G = float32(g)
b, err := strconv.ParseFloat(cs[2], 32)
if err != nil {
return sc, errors.Errorf("pdfcpu: blue must be a float value: %s\n", cs[2])
}
if b < 0 || b > 1 {
return sc, errors.New("pdfcpu: blue: a color value is an intensity between 0.0 and 1.0")
}
sc.B = float32(b)
return sc, nil
}