Skip to content

Commit ae4bc56

Browse files
Refactor design of colors in configgui.
1 parent 527cb58 commit ae4bc56

10 files changed

Lines changed: 408 additions & 1864 deletions

File tree

‎ConfigGUI/ChatPreview.cs‎

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,17 @@
1111
namespace fCraft.ConfigGUI {
1212
sealed partial class ChatPreview : UserControl {
1313

14-
struct ColorPair {
15-
public ColorPair( int r, int g, int b ) {
16-
Foreground = new SolidBrush( System.Drawing.Color.FromArgb( r, g, b ) );
17-
18-
// 25% opacity for shadow/background colour
19-
r /= 4; g /= 4; b /= 4;
20-
Shadow = new SolidBrush( System.Drawing.Color.FromArgb( r, g, b ) );
21-
}
22-
public readonly Brush Foreground, Shadow;
23-
}
24-
14+
struct ColorPair { public Brush Foreground, Shadow; }
2515
static readonly PrivateFontCollection Fonts;
2616
static readonly Font MinecraftFont;
27-
static readonly ColorPair[] ColorPairs;
17+
static Dictionary<char, ColorPair> brushes = new Dictionary<char, ColorPair>();
2818

2919
unsafe static ChatPreview() {
3020
Fonts = new PrivateFontCollection();
3121
fixed( byte* fontPointer = Resources.MinecraftFont ) {
3222
Fonts.AddMemoryFont( (IntPtr)fontPointer, Resources.MinecraftFont.Length );
3323
}
3424
MinecraftFont = new Font( Fonts.Families[0], 12, FontStyle.Regular );
35-
ColorPairs = new[]{
36-
new ColorPair(0,0,0),
37-
new ColorPair(0,0,191),
38-
new ColorPair(0,191,0),
39-
new ColorPair(0,191,191),
40-
new ColorPair(191,0,0),
41-
new ColorPair(191,0,191),
42-
new ColorPair(191,191,0),
43-
new ColorPair(191,191,191),
44-
45-
new ColorPair(64,64,64),
46-
new ColorPair(64,64,255),
47-
new ColorPair(64,255,64),
48-
new ColorPair(64,255,255),
49-
new ColorPair(255,64,64),
50-
new ColorPair(255,64,255),
51-
new ColorPair(255,255,64),
52-
new ColorPair(255,255,255)
53-
};
5425
}
5526

5627

@@ -62,12 +33,31 @@ public ChatPreview() {
6233

6334
sealed class TextSegment {
6435
public string Text;
65-
public ColorPair Color;
36+
public char ColorCode;
6637
public int X, Y;
6738

6839
public void Draw( Graphics g ) {
69-
g.DrawString( Text, MinecraftFont, Color.Shadow, X + 2, Y + 2 );
70-
g.DrawString( Text, MinecraftFont, Color.Foreground, X, Y );
40+
ColorPair pair;
41+
if( !brushes.TryGetValue( ColorCode, out pair ) ) {
42+
pair = MakeColorPair();
43+
brushes[ColorCode] = pair;
44+
}
45+
46+
g.DrawString( Text, MinecraftFont, pair.Shadow, X + 2, Y + 2 );
47+
g.DrawString( Text, MinecraftFont, pair.Foreground, X, Y );
48+
}
49+
50+
ColorPair MakeColorPair() {
51+
ColorPair pair;
52+
System.Drawing.Color textCol;
53+
54+
System.Drawing.Color c = ColorPicker.LookupColor( ColorCode, out textCol );
55+
pair.Foreground = new SolidBrush( System.Drawing.Color.FromArgb( c.R, c.G, c.B ) );
56+
57+
// 25% opacity for shadow/background colour
58+
c = System.Drawing.Color.FromArgb( c.R / 4, c.G / 4, c.B / 4 );
59+
pair.Shadow = new SolidBrush( System.Drawing.Color.FromArgb( c.R, c.G, c.B ) );
60+
return pair;
7161
}
7262
}
7363

@@ -86,19 +76,21 @@ public void SetText( string[] lines ) {
8676
int x = 5;
8777
string[] plainTextSegments = SplitByColorRegex.Split( lines[i] );
8878

89-
int color = MainForm.ParseToIndex( Color.White );
90-
79+
char colorCode = 'f';
9180
for( int j = 0; j < plainTextSegments.Length; j++ ) {
9281
if( plainTextSegments[j].Length == 0 ) continue;
9382
if( plainTextSegments[j][0] == '&' ) {
94-
color = MainForm.ParseToIndex( plainTextSegments[j] );
83+
colorCode = plainTextSegments[j][1];
84+
// Conver system color codes into actual color codes
85+
string converted = Color.Parse( colorCode );
86+
if( converted != null ) colorCode = converted[1];
9587
} else {
9688
newSegments.Add( new TextSegment {
97-
Color = ColorPairs[color],
98-
Text = plainTextSegments[j],
99-
X = x,
100-
Y = y
101-
} );
89+
ColorCode = colorCode,
90+
Text = plainTextSegments[j],
91+
X = x,
92+
Y = y
93+
} );
10294
x += (int)g.MeasureString( plainTextSegments[j], MinecraftFont ).Width;
10395
}
10496
}

0 commit comments

Comments
 (0)