forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBCodeStyle.cs
More file actions
360 lines (284 loc) · 12.2 KB
/
BBCodeStyle.cs
File metadata and controls
360 lines (284 loc) · 12.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
namespace PluginCore.BBCode
{
public class BBCodeStyle
{
#region Statics
public static BBCodeStyle fuseStyles(BBCodeStyle child, BBCodeStyle parent)
{
if (parent == null)
return child != null ? child.clone() : null;
if (child == null)
return parent.clone();
BBCodeStyle outStyle = child.clone();
if (outStyle.isBold == StateMode.DEFAULT)
outStyle.isBold = parent.isBold;
if (outStyle.isItalic == StateMode.DEFAULT)
outStyle.isItalic = parent.isItalic;
if (outStyle.isStriked == StateMode.DEFAULT)
outStyle.isStriked = parent.isStriked;
if (outStyle.isUnderlined == StateMode.DEFAULT)
outStyle.isUnderlined = parent.isUnderlined;
if (outStyle.fontSize == 0)
{
outStyle.fontSize = parent.fontSize;
outStyle.isAbsFontSize = parent.isAbsFontSize;
}
else
{
if (!outStyle.isAbsFontSize)
{
outStyle.isAbsFontSize = (bool)(parent.isAbsFontSize && parent.fontSize != 0);
outStyle.fontSize = parent.fontSize + outStyle.fontSize;
}
else
{
if (outStyle.fontSize < 0)
outStyle.fontSize = 1;
}
}
if (string.IsNullOrEmpty(outStyle.fontName))
outStyle.fontName = parent.fontName;
if (outStyle.backColor == null)
outStyle.backColor = parent.backColor;
else
outStyle.backColor = Color.Mix(parent.backColor, outStyle.backColor);
if (outStyle.foreColor == null)
outStyle.foreColor = parent.foreColor;
else
outStyle.foreColor = Color.Mix(outStyle.backColor, outStyle.foreColor);
return outStyle;
}
public static BBCodeStyle fuseStyleHierarchy(List<BBCodeStyle> parentToChildHierarchy)
{
if (parentToChildHierarchy == null || parentToChildHierarchy.Count < 1)
return null;
if (parentToChildHierarchy.Count == 1)
return parentToChildHierarchy[0].clone();
BBCodeStyle t = parentToChildHierarchy[0].clone();
if (t.foreColor == null) t.foreColor = new Color(0x000000, Mode.NORMAL);
if (t.backColor == null) t.backColor = new Color(0xCC99CC, Mode.NORMAL);
if (t.isBold == StateMode.DEFAULT) t.isBold = StateMode.OFF;
if (t.isItalic == StateMode.DEFAULT) t.isItalic = StateMode.OFF;
if (t.isStriked == StateMode.DEFAULT) t.isStriked = StateMode.OFF;
if (t.isUnderlined == StateMode.DEFAULT) t.isUnderlined = StateMode.OFF;
if (!t.isAbsFontSize)
{
t.isAbsFontSize = true;
if(t.fontSize <= 0.0f)
t.fontSize = 12.0f;
}
int i, l = parentToChildHierarchy.Count;
for (i = 1; i < l; i++)
t = fuseStyles(parentToChildHierarchy[i], t);
return t;
}
#endregion
#region Dynamics
public BBCodeStyle()
{
}
public StateMode isBold = StateMode.DEFAULT;
public StateMode isItalic = StateMode.DEFAULT;
public StateMode isStriked = StateMode.DEFAULT;
public StateMode isUnderlined = StateMode.DEFAULT;
public String fontName = null;
public float fontSize = 0;
public bool isAbsFontSize = true;
public Color foreColor = null;
public Color backColor = null;
public BBCodeStyle clone()
{
BBCodeStyle c = new BBCodeStyle();
c.isBold = this.isBold;
c.isItalic = this.isItalic;
c.isStriked = this.isStriked;
c.isUnderlined = this.isUnderlined;
c.fontName = this.fontName;
c.fontSize = this.fontSize;
c.isAbsFontSize = this.isAbsFontSize;
c.foreColor = this.foreColor == null ? null : this.foreColor.clone();
c.backColor = this.backColor == null ? null : this.backColor.clone();
return c;
}
override public String ToString()
{
return "[bbCodeStyle"
+ " isBold=" + isBold
+ " isItalic='" + isItalic + "'"
+ " isStriked='" + isStriked + "'"
+ " isUnderlined='" + isUnderlined + "'"
+ " fontName='" + (fontName == null ? "null" : fontName) + "'"
+ " fontSize='" + fontSize + "'"
+ " isAbsFontSize='" + isAbsFontSize.ToString() + "'"
+ " foreColor='" + (foreColor == null ? "null" : foreColor.ToString()) + "'"
+ " backColor='" + (backColor == null ? "null" : backColor.ToString()) + "'"
+ "]";
}
#endregion
#region SubClasses
public class Color
{
private delegate float DColorChannelMixer(float back, float fore);
#region Statics
public static Color Mix(Color back, Color fore)
{
if (back == null || fore == null)
return null;
float backR = ((float)((back.color >> 16) & 0xFF) / 255.0f);
float backG = ((float)((back.color >> 8) & 0xFF) / 255.0f);
float backB = ((float)((back.color) & 0xFF) / 255.0f);
float foreA = ((float)((fore.color >> 24) & 0xFF) / 255.0f);
float foreR = ((float)((fore.color >> 16) & 0xFF) / 255.0f);
float foreG = ((float)((fore.color >> 8) & 0xFF) / 255.0f);
float foreB = ((float)((fore.color) & 0xFF) / 255.0f);
float outR = MixChannel(backR, foreR, fore.mode);
float outG = MixChannel(backG, foreG, fore.mode);
float outB = MixChannel(backB, foreB, fore.mode);
// simplified version
outR = lerp(backR, outR, foreA);
outG = lerp(backG, outG, foreA);
outB = lerp(backB, outB, foreA);
if (outR < 0.0f) outR = 0.0f;
if (outR > 1.0f) outR = 1.0f;
if (outG < 0.0f) outG = 0.0f;
if (outG > 1.0f) outG = 1.0f;
if (outB < 0.0f) outB = 0.0f;
if (outB > 1.0f) outB = 1.0f;
return new Color((uint)( ((uint)0xFF000000)
|((uint)(255.0f * outR) << 16)
|((uint)(255.0f * outG) << 8)
|((uint)(255.0f * outB))), Mode.NORMAL);
}
public static float MixChannel(float back, float fore, Mode mode)
{
_InitStatics();
if (mode == 0)
mode = Mode.NORMAL;
return _colorChannelMixers[mode](back, fore);
}
public static Mode ResolveColorMode(String modeStr)
{
if (string.IsNullOrEmpty(modeStr))
return Mode.NORMAL;
_InitStatics();
if (_colorChannelMixersHash.ContainsKey(modeStr))
return _colorChannelMixersHash[modeStr];
return Mode.NORMAL;
}
private static float lerp(float a, float b, float pos)
{
return a + pos * (b - a);
}
private static bool _isInitedStatics = false;
private static Dictionary<Mode, DColorChannelMixer> _colorChannelMixers;
private static Dictionary<String, Mode> _colorChannelMixersHash;
private static void _InitStatics()
{
if (_isInitedStatics)
return;
_colorChannelMixers = new Dictionary<Mode, DColorChannelMixer>();
_colorChannelMixers[Mode.NORMAL] = new DColorChannelMixer(_channelMixer_NORMAL);
_colorChannelMixers[Mode.ADD] = new DColorChannelMixer(_channelMixer_ADD);
_colorChannelMixers[Mode.SUBTRACT] = new DColorChannelMixer(_channelMixer_SUBTRACT);
_colorChannelMixers[Mode.MULTIPLY] = new DColorChannelMixer(_channelMixer_MULTIPLY);
_colorChannelMixers[Mode.DIVIDE] = new DColorChannelMixer(_channelMixer_DIVIDE);
_colorChannelMixers[Mode.DIFFERENCE] = new DColorChannelMixer(_channelMixer_DIFFERENCE);
_colorChannelMixers[Mode.EXCLUSION] = new DColorChannelMixer(_channelMixer_EXCLUSION);
_colorChannelMixers[Mode.OVERLAY] = new DColorChannelMixer(_channelMixer_OVERLAY);
_colorChannelMixers[Mode.HARDLIGHT] = new DColorChannelMixer(_channelMixer_HARDLIGHT);
_colorChannelMixersHash = new Dictionary<string, Mode>();
_colorChannelMixersHash["NORMAL"] = Mode.NORMAL;
_colorChannelMixersHash["ADD"] = Mode.ADD;
_colorChannelMixersHash["SUBTRACT"] = Mode.SUBTRACT;
_colorChannelMixersHash["MULTIPLY"] = Mode.MULTIPLY;
_colorChannelMixersHash["DIVIDE"] = Mode.DIVIDE;
_colorChannelMixersHash["DIFFERENCE"] = Mode.DIFFERENCE;
_colorChannelMixersHash["EXCLUSION"] = Mode.EXCLUSION;
_colorChannelMixersHash["OVERLAY"] = Mode.OVERLAY;
_colorChannelMixersHash["HARDLIGHT"] = Mode.HARDLIGHT;
_isInitedStatics = true;
}
private static float _channelMixer_NORMAL(float back, float fore)
{
return fore;
}
private static float _channelMixer_ADD(float back, float fore)
{
return back + fore;
}
private static float _channelMixer_SUBTRACT(float back, float fore)
{
return back + fore - 1.0f;
}
private static float _channelMixer_MULTIPLY(float back, float fore)
{
return back * fore;
}
private static float _channelMixer_DIVIDE(float back, float fore)
{
return back / fore;
}
private static float _channelMixer_DIFFERENCE(float back, float fore)
{
return Math.Abs(back - fore);
}
private static float _channelMixer_EXCLUSION(float back, float fore)
{
return back + fore - 2.0f * back * fore;
}
private static float _channelMixer_OVERLAY(float back, float fore)
{
if (back < 0.5)
return 2.0f * back * fore;
return 2.0f * (0.5f - (1.0f - fore) * (1.0f - back));
}
private static float _channelMixer_HARDLIGHT(float back, float fore)
{
return _channelMixer_OVERLAY(fore, back);
}
#endregion
public Color()
{
}
public Color(uint color)
{
this.color = color;
}
public Color(uint color, Mode mode)
{
this.color = color;
this.mode = mode;
}
public uint color = 0xFF000000;
public Mode mode = Mode.NORMAL;
public Color clone()
{
return new Color(color, mode);
}
public override string ToString()
{
return "[bbCodeStyle.Color"
+ " color=" + color.ToString("X")
+ " mode='" + mode + "'"
+ "]";
}
}
#region SubClasses
public enum Mode : uint
{
NORMAL = 0,
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4,
DIFFERENCE = 5,
EXCLUSION = 6,
OVERLAY = 7,
HARDLIGHT = 8
}
#endregion
#endregion
}
}