forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPM.cs
More file actions
59 lines (56 loc) · 1.62 KB
/
Copy pathXPM.cs
File metadata and controls
59 lines (56 loc) · 1.62 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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
namespace ScintillaNet
{
public class XPM
{
/// <summary>
/// Converts Bitmap images to XPM data for use with ScintillaNet.
/// Warning: images with more than (around) 50 colors will generate incorrect XPM
/// tColor: specified transparent color in format: "#00FF00".
/// </summary>
static public string ConvertToXPM(Bitmap bmp, string tColor)
{
StringBuilder sb = new StringBuilder();
ArrayList colors = new ArrayList();
ArrayList chars = new ArrayList();
int width = bmp.Width;
int height = bmp.Height;
int index;
sb.Append("/* XPM */static char * xmp_data[] = {\"").Append(width).Append(" ").Append(height).Append(" ? 1\"");
int colorsIndex = sb.Length;
string col;
char c;
for (int y = 0; y<height; y++)
{
sb.Append(",\"");
for (int x = 0; x<width; x++)
{
col = ColorTranslator.ToHtml(bmp.GetPixel(x,y));
index = colors.IndexOf(col);
if (index < 0)
{
index = colors.Add(col)+65;
if (index > 90) index += 6;
c = Encoding.ASCII.GetChars(new byte[]{(byte)(index & 0xff)})[0];
chars.Add(c);
sb.Insert(colorsIndex, ",\""+c+" c "+col+"\"");
colorsIndex += 14;
}
else c = (char)chars[index];
sb.Append(c);
}
sb.Append("\"");
}
sb.Append("};");
string result = sb.ToString();
int p = result.IndexOf("?");
string finalColor = result.Substring(0, p)+colors.Count+result.Substring(p+1).Replace(tColor.ToUpper(), "None");
return finalColor;
}
}
}