vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | local MAJOR_VERSION = "1.0" |
2 | local MINOR_VERSION = tonumber(string.sub("$Revision: 2427 $", 12, -3)) |
||
3 | if CrayonLib and CrayonLib.versions[MAJOR_VERSION] and CrayonLib.versions[MAJOR_VERSION].minor >= MINOR_VERSION then |
||
4 | return |
||
5 | end |
||
6 | |||
7 | -------------IRIEL'S-STUB-CODE-------------- |
||
8 | local stub = {}; |
||
9 | |||
10 | -- Instance replacement method, replace contents of old with that of new |
||
11 | function stub:ReplaceInstance(old, new) |
||
12 | for k,v in pairs(old) do old[k]=nil; end |
||
13 | for k,v in pairs(new) do old[k]=v; end |
||
14 | end |
||
15 | |||
16 | -- Get a new copy of the stub |
||
17 | function stub:NewStub() |
||
18 | local newStub = {}; |
||
19 | self:ReplaceInstance(newStub, self); |
||
20 | newStub.lastVersion = ''; |
||
21 | newStub.versions = {}; |
||
22 | return newStub; |
||
23 | end |
||
24 | |||
25 | -- Get instance version |
||
26 | function stub:GetInstance(version) |
||
27 | if (not version) then version = self.lastVersion; end |
||
28 | local versionData = self.versions[version]; |
||
29 | if (not versionData) then |
||
30 | message("Cannot find library instance with version '" |
||
31 | .. version .. "'"); |
||
32 | return; |
||
33 | end |
||
34 | return versionData.instance; |
||
35 | end |
||
36 | |||
37 | -- Register new instance |
||
38 | function stub:Register(newInstance) |
||
39 | local version,minor = newInstance:GetLibraryVersion(); |
||
40 | self.lastVersion = version; |
||
41 | local versionData = self.versions[version]; |
||
42 | if (not versionData) then |
||
43 | -- This one is new! |
||
44 | versionData = { instance = newInstance, |
||
45 | minor = minor, |
||
46 | old = {} |
||
47 | }; |
||
48 | self.versions[version] = versionData; |
||
49 | newInstance:LibActivate(self); |
||
50 | return newInstance; |
||
51 | end |
||
52 | if (minor <= versionData.minor) then |
||
53 | -- This one is already obsolete |
||
54 | if (newInstance.LibDiscard) then |
||
55 | newInstance:LibDiscard(); |
||
56 | end |
||
57 | return versionData.instance; |
||
58 | end |
||
59 | -- This is an update |
||
60 | local oldInstance = versionData.instance; |
||
61 | local oldList = versionData.old; |
||
62 | versionData.instance = newInstance; |
||
63 | versionData.minor = minor; |
||
64 | local skipCopy = newInstance:LibActivate(self, oldInstance, oldList); |
||
65 | table.insert(oldList, oldInstance); |
||
66 | if (not skipCopy) then |
||
67 | for i, old in ipairs(oldList) do |
||
68 | self:ReplaceInstance(old, newInstance); |
||
69 | end |
||
70 | end |
||
71 | return newInstance; |
||
72 | end |
||
73 | |||
74 | -- Bind stub to global scope if it's not already there |
||
75 | if (not CrayonLib) then |
||
76 | CrayonLib = stub:NewStub(); |
||
77 | end |
||
78 | |||
79 | -- Nil stub for garbage collection |
||
80 | stub = nil; |
||
81 | -----------END-IRIEL'S-STUB-CODE------------ |
||
82 | |||
83 | local function assert(condition, message) |
||
84 | if not condition then |
||
85 | local stack = debugstack() |
||
86 | local first = string.gsub(stack, "\n.*", "") |
||
87 | local file = string.gsub(first, "^(.*\\.*).lua:%d+: .*", "%1") |
||
88 | file = string.gsub(file, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1") |
||
89 | if not message then |
||
90 | local _,_,second = string.find(stack, "\n(.-)\n") |
||
91 | message = "assertion failed! " .. second |
||
92 | end |
||
93 | message = "CrayonLib: " .. message |
||
94 | local i = 1 |
||
95 | for s in string.gfind(stack, "\n([^\n]*)") do |
||
96 | i = i + 1 |
||
97 | if not string.find(s, file .. "%.lua:%d+:") then |
||
98 | error(message, i) |
||
99 | return |
||
100 | end |
||
101 | end |
||
102 | error(message, 2) |
||
103 | return |
||
104 | end |
||
105 | return condition |
||
106 | end |
||
107 | |||
108 | local function argCheck(arg, num, kind, kind2, kind3, kind4) |
||
109 | if tostring(type(arg)) ~= kind then |
||
110 | if kind2 then |
||
111 | if tostring(type(arg)) ~= kind2 then |
||
112 | if kind3 then |
||
113 | if tostring(type(arg)) ~= kind3 then |
||
114 | if kind4 then |
||
115 | if tostring(type(arg)) ~= kind4 then |
||
116 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
117 | assert(false, format("Bad argument #%d to `%s' (%s, %s, %s, or %s expected, got %s)", num, func, kind, kind2, kind3, kind4, type(arg))) |
||
118 | end |
||
119 | else |
||
120 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
121 | assert(false, format("Bad argument #%d to `%s' (%s, %s, or %s expected, got %s)", num, func, kind, kind2, kind3, type(arg))) |
||
122 | end |
||
123 | end |
||
124 | else |
||
125 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
126 | assert(false, format("Bad argument #%d to `%s' (%s or %s expected, got %s)", num, func, kind, kind2, type(arg))) |
||
127 | end |
||
128 | end |
||
129 | else |
||
130 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
131 | assert(false, format("Bad argument #%d to `%s' (%s expected, got %s)", num, func, kind, type(arg))) |
||
132 | end |
||
133 | end |
||
134 | end |
||
135 | |||
136 | local lib = {} |
||
137 | |||
138 | lib.COLOR_HEX_RED = "ff0000" |
||
139 | lib.COLOR_HEX_ORANGE = "ff7f00" |
||
140 | lib.COLOR_HEX_YELLOW = "ffff00" |
||
141 | lib.COLOR_HEX_GREEN = "00ff00" |
||
142 | lib.COLOR_HEX_WHITE = "ffffff" |
||
143 | lib.COLOR_HEX_COPPER = "eda55f" |
||
144 | lib.COLOR_HEX_SILVER = "c7c7cf" |
||
145 | lib.COLOR_HEX_GOLD = "ffd700" |
||
146 | |||
147 | function lib:Colorize(hexColor, text) |
||
148 | return "|cff" .. tostring(hexColor or 'ffffff') .. tostring(text) .. "|r" |
||
149 | end |
||
150 | function lib:Red(text) return self:Colorize(self.COLOR_HEX_RED, text) end |
||
151 | function lib:Orange(text) return self:Colorize(self.COLOR_HEX_ORANGE, text) end |
||
152 | function lib:Yellow(text) return self:Colorize(self.COLOR_HEX_YELLOW, text) end |
||
153 | function lib:Green(text) return self:Colorize(self.COLOR_HEX_GREEN, text) end |
||
154 | function lib:White(text) return self:Colorize(self.COLOR_HEX_WHITE, text) end |
||
155 | function lib:Copper(text) return self:Colorize(self.COLOR_HEX_COPPER, text) end |
||
156 | function lib:Silver(text) return self:Colorize(self.COLOR_HEX_SILVER, text) end |
||
157 | function lib:Gold(text) return self:Colorize(self.COLOR_HEX_GOLD, text) end |
||
158 | |||
159 | local inf = 1/0 |
||
160 | |||
161 | function lib:GetThresholdColor(quality, worst, worse, normal, better, best) |
||
162 | argCheck(quality, 2, "number") |
||
163 | if quality ~= quality or quality == inf or quality == -inf then |
||
164 | return 1, 1, 1 |
||
165 | end |
||
166 | if not best then |
||
167 | worst = 0 |
||
168 | worse = 0.25 |
||
169 | normal = 0.5 |
||
170 | better = 0.75 |
||
171 | best = 1 |
||
172 | end |
||
173 | |||
174 | if worst < best then |
||
175 | if (worse == better and quality == worse) or (worst == best and quality == worst) then |
||
176 | return 1, 1, 0 |
||
177 | elseif quality <= worst then |
||
178 | return 1, 0, 0 |
||
179 | elseif quality <= worse then |
||
180 | return 1, 0.5 * (quality - worst) / (worse - worst), 0 |
||
181 | elseif quality <= normal then |
||
182 | return 1, 0.5 + 0.5 * (quality - worse) / (normal - worse), 0 |
||
183 | elseif quality <= better then |
||
184 | return 1 - 0.5 * (quality - normal) / (better - normal), 1, 0 |
||
185 | elseif quality <= best then |
||
186 | return 0.5 - 0.5 * (quality - better) / (best - better), 1, 0 |
||
187 | else |
||
188 | return 0, 1, 0 |
||
189 | end |
||
190 | else |
||
191 | if (worse == better and quality == worse) or (worst == best and quality == worst) then |
||
192 | return 1, 1, 0 |
||
193 | elseif quality >= worst then |
||
194 | return 1, 0, 0 |
||
195 | elseif quality >= worse then |
||
196 | return 1, 0.5 - 0.5 * (quality - worse) / (worst - worse), 0 |
||
197 | elseif quality >= normal then |
||
198 | return 1, 1 - 0.5 * (quality - normal) / (worse - normal), 0 |
||
199 | elseif quality >= better then |
||
200 | return 0.5 + 0.5 * (quality - better) / (normal - better), 1, 0 |
||
201 | elseif quality >= best then |
||
202 | return 0.5 * (quality - best) / (better - best), 1, 0 |
||
203 | else |
||
204 | return 0, 1, 0 |
||
205 | end |
||
206 | end |
||
207 | end |
||
208 | |||
209 | function lib:GetThresholdHexColor(quality, worst, worse, normal, better, best) |
||
210 | local r, g, b = self:GetThresholdColor(quality, worst, worse, normal, better, best) |
||
211 | return format("%02x%02x%02x", r*255, g*255, b*255) |
||
212 | end |
||
213 | |||
214 | function lib:GetThresholdColorTrivial(quality, worst, worse, normal, better, best) |
||
215 | argCheck(quality, 2, "number") |
||
216 | if quality ~= quality or quality == inf or quality == -inf then |
||
217 | return 1, 1, 1 |
||
218 | end |
||
219 | if not best then |
||
220 | worst = 0 |
||
221 | worse = 0.25 |
||
222 | normal = 0.5 |
||
223 | better = 0.75 |
||
224 | best = 1 |
||
225 | end |
||
226 | |||
227 | if worst < best then |
||
228 | if worse == better and normal == worse then |
||
229 | return 1, 1, 0 |
||
230 | elseif quality <= worst then |
||
231 | return 1, 0, 0 |
||
232 | elseif quality <= worse then |
||
233 | return 1, 0.5 * (quality - worst) / (worse - worst), 0 |
||
234 | elseif quality <= normal then |
||
235 | return 1, 0.5 + 0.5 * (quality - worse) / (normal - worse), 0 |
||
236 | elseif quality <= better then |
||
237 | return 1 - (quality - normal) / (better - normal), 1, 0 |
||
238 | elseif quality <= best then |
||
239 | local x = 0.5 * (quality - better) / (best - better) |
||
240 | return x, 1 - x, x |
||
241 | else |
||
242 | return 0.5, 0.5, 0.5 |
||
243 | end |
||
244 | else |
||
245 | if worse == better and normal == worse then |
||
246 | return 1, 1, 0 |
||
247 | elseif quality >= worst then |
||
248 | return 1, 0, 0 |
||
249 | elseif quality >= worse then |
||
250 | return 1, 0.5 - 0.5 * (quality - worse) / (worst - worse), 0 |
||
251 | elseif quality >= normal then |
||
252 | return 1, 1 - 0.5 * (quality - normal) / (worse - normal), 0 |
||
253 | elseif quality >= better then |
||
254 | return (quality - better) / (normal - better), 1, 0 |
||
255 | elseif quality >= best then |
||
256 | local x = 0.5 * (quality - best) / (better - best) |
||
257 | return 0.5 - x, 0.5 + x, 0.5 - x |
||
258 | else |
||
259 | return 0.5, 0.5, 0.5 |
||
260 | end |
||
261 | end |
||
262 | end |
||
263 | |||
264 | function lib:GetThresholdHexColorTrivial(quality, worst, worse, normal, better, best) |
||
265 | local r, g, b = self:GetThresholdColorTrivial(quality, worst, worse, normal, better, best) |
||
266 | return format("%02x%02x%02x", r*255, g*255, b*255) |
||
267 | end |
||
268 | |||
269 | function lib:GetLibraryVersion() |
||
270 | return MAJOR_VERSION, MINOR_VERSION |
||
271 | end |
||
272 | |||
273 | function lib:LibActivate(stub, oldLib, oldList) |
||
274 | end |
||
275 | |||
276 | function lib:LibDeactivate(stub) |
||
277 | end |
||
278 | |||
279 | CrayonLib:Register(lib) |
||
280 | lib = nil |