-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFUtils_Color.lua
More file actions
267 lines (233 loc) · 7.77 KB
/
SFUtils_Color.lua
File metadata and controls
267 lines (233 loc) · 7.77 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
-- LibSFUtils is already defined in prior loaded file
local sfutil = LibSFUtils or {}
---------------------
--[[
Color conversion functions
]]
-- Turn a ([0,1])^3 RGB colour to "ABCDEF" form.
function sfutil.colorRGBToHex(r, g, b)
if not r or not g or not b then return nil end
return string.format("%.2x%.2x%.2x", zo_floor((tonumber(r) or 1) * 255),
zo_floor((tonumber(g) or 1) * 255), zo_floor((tonumber(b) or 1) * 255))
end
-- Convert "rrggbb" hex color into float numbers.
-- Prefer sfutil.ConvertHexToRGBA() as it is more flexible.
function sfutil.colorHexToRGBA(colourString)
if not colourString then
return 1,1,1,1
end
local r=tonumber(string.sub(colourString, 1, 2), 16) or 255
local g=tonumber(string.sub(colourString, 3, 4), 16) or 255
local b=tonumber(string.sub(colourString, 5, 6), 16) or 255
return r/255, g/255, b/255, 1
end
-- Turn a ([0,1])^3 RGB colour to "|cABCDEF" form. We could use
-- ZO_ColorDef to build this, but we use so many colors, we won't do it.
-- Note: This is NOT the same as the LibSFUtils.colorRGBToHex() function!
function sfutil.ConvertRGBToHex(r, g, b)
return string.format("|c%.2x%.2x%.2x", zo_floor(r * 255), zo_floor(g * 255), zo_floor(b * 255))
end
-- Convert a colour from hexadecimal form to {[0,1],[0,1],[0,1]} RGB form.
-- Note: This is NOT the same as the older LibSFUtils.colorHexToRGBA() function
-- as it can convert from a variety of hex string formats for colors:
-- |crrggbb, aarrggbb, and rrggbb
function sfutil.ConvertHexToRGBA(colourString)
if type(colourString) ~= "string" then
return 1,1,1,1
end
local r, g, b, a
if string.sub(colourString,1,1) == "|" then
-- format "|crrggbb"
r=tonumber(string.sub(colourString, 3, 4), 16) or 255
g=tonumber(string.sub(colourString, 5, 6), 16) or 255
b=tonumber(string.sub(colourString, 7, 8), 16) or 255
a = 255
elseif #colourString == 8 then
-- format "aarrggbb"
a=tonumber(string.sub(colourString, 1, 2), 16) or 255
r=tonumber(string.sub(colourString, 3, 4), 16) or 255
g=tonumber(string.sub(colourString, 5, 6), 16) or 255
b=tonumber(string.sub(colourString, 7, 8), 16) or 255
elseif #colourString == 6 then
-- format "rrggbb"
r=tonumber(string.sub(colourString, 1, 2), 16) or 255
g=tonumber(string.sub(colourString, 3, 4), 16) or 255
b=tonumber(string.sub(colourString, 5, 6), 16) or 255
a = 255
else
-- unidentified format
r = 255
g = 255
b = 255
a = 255
end
return r/255, g/255, b/255, a/255
end
-- Convert a colour from "|cABCDEF" form to [0,1] RGB form and return them in a table.
function sfutil.ConvertHexToRGBAPacked(colourString)
local r, g, b, a = sfutil.ConvertHexToRGBA(colourString)
return {r = r, g = g, b = b, a = a}
end
-- ------------------------------------------
SF_Color = ZO_Object:Subclass()
--[[
Don't want to make this public because it can leave
SF_Color in an inconsistant state - hex is not set
from these values. We just assume that has been or
will be taken care of.
--]]
local function setRGB(sfcolor, r, g, b, a)
r = r>1 and r/255 or r
g = g>1 and g/255 or g
b = b>1 and b/255 or b
a = a>1 and a/255 or a
sfcolor.rgb.r = r or 1
sfcolor.rgb.g = g or 1
sfcolor.rgb.b = b or 1
sfcolor.rgb.a = a or 1
end
--[[ ---------------------
Create a color object.
This is a storage container for:
hex - a 6-character hex representation of the RGB color
rgb - a table containing the float values for r, g, b, a (values btwn 0-1)
with some handy related functions.
Why not use the already existing ZO_ColorDef? The intention is to have an object
which does not do as much calculation behind the scenes with every use - with the
intention of optimizing speed at the expense of a little extra memory.
The capability to convert between one and the other is provided.
Parameters options:
pr, pg, pb, pa - The RGB floats between 0-1.
Missing (nil) rgba values will be set to 1.
pr - The hex value (6-character string rrggbb) to set the color to.
pr - The hex value (8-character string aarrggbb) to set the color to.
pr - Another SF_Color object to copy values from.
pr - A ZO_ColorDef to copy/calculate values from.
--]]
function SF_Color:New(pr, pg, pb, pa)
local c = ZO_Object.New(self)
c.rgb = {r=1, g=1, b=1, a=1}
c:SetColor(pr, pg, pb, pa)
return c
end
--[[ ---------------------
returns the color values r, g, b (in that order)
color values are in [0,1]
--]]
function SF_Color:UnpackRGB()
if not self.rgb and self.r then
return self.r, self.g, self.b
end
return self.rgb.r, self.rgb.g, self.rgb.b
end
--[[ ---------------------
returns the color values r, g, b, a (in that order)
color values are in [0,1]
--]]
function SF_Color:UnpackRGBA()
if not self.rgb and self.r then
return self.r, self.g, self.b, self.a or 1
end
return self.rgb.r or 1, self.rgb.g or 1, self.rgb.b or 1, self.rgb.a or 1
end
function SF_Color:SetAlpha(a)
if self.rgb then
self.rgb.a = a
elseif self.r then
self.a = a
end
end
--[[ ---------------------
Set a color object to a particular color value.
Parameters:
pr, pg, pb, pa - The RGB floats between 0-1.
Missing (nil) rgba values will be set to 1.
pr - The hex value (6-character string) to set the color to.
pr - Another SF_Color object to copy values from.
pr - A ZOS ZO_ColorDef to copy/calculate values from.
--]]
function SF_Color:SetColor(r, g, b, a)
if type(r) == "string" then
-- r is hex value
self.hex = r
self.rgb.r, self.rgb.g, self.rgb.b, self.rgb.a = sfutil.ConvertHexToRGBA(r)
elseif type(r) == "table" then
if r.r ~= nil then
-- r is ZO_ColorDef
setRGB(self, r:UnpackRGBA())
self.hex = sfutil.colorRGBToHex(r:UnpackRGB())
else
-- r is SF_Color we are copying
setRGB(self, r:UnpackRGBA())
self.hex = r.hex
end
elseif r > 1 or g > 1 or b > 1 or a > 1 then
-- r is RGB value (1,255]
setRGB(self, r/255, g/255, b/255, a/255)
self.hex = sfutil.colorRGBToHex(self:UnpackRGB())
else
-- r is float value [0,1]
setRGB(self, r, g, b, a)
self.hex = sfutil.colorRGBToHex(self:UnpackRGB())
end
return self
end
--[[ ---------------------
Create a ZO_ColorDef object with the same color values
as are in the SF_Color object.
Returns a ZO_ColorDef object.
--]]
function SF_Color:ToZO_ColorDef()
return ZO_ColorDef:New(self:UnpackRGBA())
end
--[[ ---------------------
Add the colorizing markup to a string of text for display
Parameter:
text - string text to be colorized, or
text - number to use GetString() on to get text to color
Return:
string - colorized text
--]]
function SF_Color:Colorize(text)
local strprompt
if( text == nil ) then
return "" -- Do NOT colorize an empty string!
elseif( type(text) == "string") then
strprompt = text
elseif( type(text) == "number") then
strprompt = GetString(text)
else
strprompt = tostring(text)
end
--if self then
-- if self.rgb then d("r="..self.rgb.r.." g="..self.rgb.g.." b="..self.rgb.b) end
-- if self.hex then d("color to colorize = 0x"..self.hex) end
--end
local combineTable = { "|c", self.hex, strprompt, "|r" }
return table.concat(combineTable)
end
--[[ ---------------------
Determine if the color values are equivalent (even if the structures are not).
Paramenter:
other - a ZO_ColorDef object, or
other - a SF_Color object
--]]
function SF_Color:IsEqual(other)
if other.r ~= nil then
-- ZO_ColorDef
return self.rgb.r == other.r
and self.rgb.g == other.g
and self.rgb.b == other.b
and self.rgb.a == other.a
end
-- SF_Color
return self.hex == other.hex
and self.rgb.a == other.rgb.a
end
function SF_Color:Clone()
return SF_Color:New(self:UnpackRGBA())
end
function SF_Color:ToHex()
return self.hex
end
sfutil.SF_Color = SF_Color