-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorCoder.cs
150 lines (129 loc) · 4.49 KB
/
ColorCoder.cs
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
using UnityEngine;
using System.Collections.Generic;
public class ColorCoder
{
//Color schemes:::
public static Color Color_keyword = new Color(88 / 255f, 156 / 255f, 214 / 255f);
public static Color Color_object = new Color(80 / 255f, 200 / 255f, 175 / 255f);
public static Color Color_comment = new Color(88 / 255f, 166 / 255f, 77 / 255f);
public static Color Color_string = new Color(210 / 255f, 150 / 255f, 100 / 255f);
public static Color Color_highlight = new Color(1, 0, 0);
public static string[] keyword_list = { "boolean", "char", "class", "const", "double", "else", "final", "float", "for", "if", "int", "new", "private", "public", "return", "static", "this", "void", "while" };
public static string[] object_list = { "PLACEHOLDER_FOR_CLASS_NAME", "Color", "Position", "String", "System"};
public static string colorize(string line)
{
List<int> indexes;
//STRINGS
line = colorizeStrings(line);
//KEYWORDS
for (int i = 0; i < keyword_list.Length; i++)
{
indexes = Parser.findWordLocations(line, keyword_list[i]);
line = colorizeWords(line, keyword_list[i], indexes, Color_keyword);
}
//OBJECTS
for (int i = 0; i < object_list.Length; i++)
{
indexes = Parser.findWordLocations(line, object_list[i]);
line = colorizeWords(line, object_list[i], indexes, Color_object);
}
return line;
}
public static string colorizeStrings(string line)
{
if (!line.Contains("\""))
{
return line;
}
else
{
string output = "";
bool open_string = false;
for (int i = 0; i < line.Length; i++)
{
if (line[i] == '\"')
{
if (!open_string)
{
output += "<color=#" + getStringColor() + ">";
}
else
{
output += "\""+ "</color>";
i++;
}
open_string = !open_string;
}
output += line[i];
}
return output;
}
}
public static string colorizeWords(string line, string word, List<int> indexes, Color color_in)
{
if (indexes.Count == 0)
{
return line;
}
else
{
string output = "";
int shifter = 0;
for (int i = 0; i < indexes.Count; i++)
{
output += line.Substring(shifter, indexes[i]) + "<color=#" + RGBToHex(color_in) + ">" + word + "</color>" + line.Substring(indexes[i] + word.Length);
shifter = indexes[i];
}
return output;
}
}
public static string highlight(string line)
{
return "<mark=#" + ColorCoder.getHighlightColor() + "55>" + line + "</mark>";
}
public static string comment(string line)
{
return line.Substring(0, line.IndexOf("//")) + "<color=#" + ColorCoder.getCommentColor() + ">" + line.Substring(line.IndexOf("//")) + "</color>";
}
public static string getKeywordColor()
{
return RGBToHex(Color_keyword);
}
public static string getCommentColor()
{
return RGBToHex(Color_comment);
}
public static string getHighlightColor()
{
return RGBToHex(Color_highlight);
}
public static string getColorObject()
{
return RGBToHex(Color_object);
}
public static string getStringColor()
{
return RGBToHex(Color_string);
}
/* Code converted from Danny Lawrence's JavaScript implementation
http://wiki.unity3d.com/index.php?title=HexConverter */
public static string RGBToHex(Color color_in)
{
float red = color_in.r * 255f;
float green = color_in.g * 255f;
float blue = color_in.b * 255f;
string a = GetHex(Mathf.Floor(red / 16));
string b = GetHex(Mathf.Round(red % 16));
string c = GetHex(Mathf.Floor(green / 16));
string d = GetHex(Mathf.Round(green % 16));
string e = GetHex(Mathf.Floor(blue / 16));
string f = GetHex(Mathf.Round(blue % 16));
string z = a + b + c + d + e + f;
return z;
}
public static string GetHex(float value)
{
string hex_values = "0123456789ABCDEF";
return hex_values[(int)value] + "";
}
}