vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 --
3 -- Sea.math
4 --
5 -- Useful math constants and values
6 --
7 -- $LastChangedBy: legorol $
8 -- $Rev: 2845 $
9 -- $Date: 2005-12-07 21:32:15 -0600 (Wed, 07 Dec 2005) $
10 --]]
11  
12 Sea.math = {
13  
14 -- Traditional pi
15 pi = math.pi;
16  
17 --
18 -- rgbaFromHex(string hex)
19 --
20 -- Convert a hex string "AARRGGBB" into floats
21 --
22 -- Args:
23 -- (string hex)
24 -- hex - the string in hexidecimal
25 --
26 -- Returns:
27 -- (Number red, number blue, number green, number alpha)
28 --
29 rgbaFromHex = function (hexColor)
30 local alpha, red, green, blue = 1;
31 local intFromHex = Sea.math.intFromHex;
32 local ssub = string.sub;
33 -- Handle short strings
34 if ( string.len(hexColor) > 6 ) then
35 alpha = intFromHex(ssub(hexColor, 1, 2)) / 255;
36 red = intFromHex(ssub(hexColor, 3, 4)) / 255;
37 green = intFromHex(ssub(hexColor, 5, 6)) / 255;
38 blue = intFromHex(ssub(hexColor, 7, 8)) / 255;
39 else
40 red = intFromHex(ssub(hexColor, 1, 2)) / 255;
41 green = intFromHex(ssub(hexColor, 3, 4)) / 255;
42 blue = intFromHex(ssub(hexColor, 5, 6)) / 255;
43 end
44  
45 return red, green, blue, alpha;
46 end;
47  
48 --
49 -- intFromHex(string hex)
50 --
51 -- Convert a hex string into an integer.
52 --
53 -- Args:
54 -- (string hex)
55 -- hex - the string in hexidecimal
56 --
57 -- Returns:
58 -- (number value)
59 -- value - the number as an integer
60 --
61 -- Recoded by Iriel (iriel@vigilance-committee.org) for performance
62 intFromHex = function ( hexCode )
63 local intFromHexTable = Sea.math.intFromHexTable;
64 local amount = 0;
65 local len = string.len(hexCode)
66 local ssub = string.sub;
67 local c;
68 for i=1,len do
69 c = ssub(hexCode, i, i);
70 amount = amount * 16 + (intFromHexTable[c] or 0);
71 end
72 return amount;
73 end;
74  
75 intFromHexTable = {
76 ["0"] = 0, ["1"] = 1, ["2"] = 2, ["3"] = 3, ["4"] = 4,
77 ["5"] = 5, ["6"] = 6, ["7"] = 7, ["8"] = 8, ["9"] = 9,
78 ["A"] = 10, ["B"] = 11, ["C"] = 12,
79 ["D"] = 13, ["E"] = 14, ["F"] = 15,
80 ["a"] = 10, ["b"] = 11, ["c"] = 12,
81 ["d"] = 13, ["e"] = 14, ["f"] = 15,
82 };
83  
84 --
85 -- hexFromInt(Num int [, minlength])
86 --
87 -- Converts a decimal to hex string
88 --
89 -- Args:
90 -- (Number int)
91 -- int - the value in decimal
92 -- minlength - the zero padding
93 --
94 -- Returns:
95 -- (String hex)
96 -- hex - the value in hex
97 --
98 hexFromInt = function (intval, minlength)
99 local fmt = "%.2x"
100 if ( minlength ) then
101 fmt = "%."..minlength.."x";
102 end
103 return string.format(fmt, intval );
104 end;
105  
106  
107 --
108 -- convertBase(string input, int original, int outputBase)
109 -----------------------------------------------------------
110 -- Function made by KaTTaNa ! --
111 -- -------------------------- --
112 -- http://www.wc3sear.ch/index.php?p=JASS&ID=37&sid= --
113 -- -------------------------- --
114 -- Converted in LUA by vjeux --
115 -- --
116 -- Usage : BaseConversion(255, 10, 16) --
117 -- => Return "ff" --
118 -- --
119 -- Usage : BaseConversion("ff", 16, 10) --
120 -- => Return "255" --
121 -----------------------------------------------------------
122 --
123 convertBase = function (input, inputBase, outputBase)
124 local charMap = "0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+-=[]";
125 local s;
126 local result = "";
127 local val = 0;
128 local i;
129 local p = 0;
130 local pow = 1;
131 local sign = "";
132  
133 if ( inputBase < 2 or inputBase > string.len(charMap) or outputBase < 2 or outputBase > string.len(charMap) ) then
134 -- Bases are invalid or out of bounds
135 return "Invalid bases given";
136 end
137 if ( string.sub(input, 1, 1) == "-" ) then
138 sign = "-";
139 input = string.sub(input, 1, string.len(input));
140 end
141 i = string.len(input);
142 -- Get the integer value of input
143 while (i > 0) do
144 s = string.sub(input, i, i);
145 p = 0;
146 local bool = false;
147 while (bool == false) do
148 if ( p >= inputBase ) then
149 -- Input cannot match base
150 return "Input does not match base!\n P = "..p;
151 end
152 if ( s == string.sub(charMap, p+1, p+1) ) then
153 val = val + pow * p;
154 pow = pow * inputBase;
155 bool = true;
156 end
157 p = p + 1;
158 end
159 i = i - 1;
160 end
161 while (val > 0) do
162 p = math.mod(val, outputBase);
163 result = string.sub(charMap, p+1, p+1)..result;
164 val = val / outputBase;
165 end
166  
167 for i = 1, string.len(result), 1 do
168 if (string.sub(result, 1, 1) == "0") then
169 result = string.sub(result, 2, string.len(result));
170 else
171 return sign..result
172 end
173 end
174  
175 if (string.len(sign..result) == 0) then
176 return "0";
177 else
178 return sign..result.."-"..string.len(sign..result);
179 end
180 end;
181  
182  
183 -- round(float x)
184 --
185 -- Rounds a float value to the "closest" integer (following IEEE standard).
186 --
187 -- Args:
188 -- (float x)
189 -- x - the value to round
190 --
191 -- Returns:
192 -- (number value)
193 -- value - the number as an integer (the closest integer to x)
194 round = function (x)
195 if (x < 0) then
196 return math.ceil(x-0.5)
197 else
198 return math.floor(x+0.5)
199 end
200 end
201  
202 };