Allow using 3 and 6 char codes for hex colours.

This commit is contained in:
UnknownShadow200 2018-03-01 08:17:33 +11:00
parent 99da2183ca
commit 4f4427b357
9 changed files with 42 additions and 48 deletions

View File

@ -314,13 +314,16 @@ namespace MCGalaxy {
/// <summary> Parses an #RRGGBB hex color string. </summary>
public static ColorDesc ParseHex(string hex) {
if (hex.Length > 0 && hex[0] == '#') hex = hex.Remove(0, 1);
if (!(hex.Length == 3 || hex.Length == 6)) {
throw new ArgumentException("hex must be either 3 or 6 chars long");
public static bool TryParseHex(string hex, out ColorDesc c) {
c = default(ColorDesc);
if (hex == null || hex.Length == 0) return false;
if (hex[0] == '#') hex = hex.Remove(0, 1);
if (!(hex.Length == 3 || hex.Length == 6)) return false;
for (int i = 0; i < hex.Length; i++) {
if (Hex(hex[i]) == -1) return false;
}
ColorDesc c = default(ColorDesc);
int R, G, B;
if (hex.Length == 6) {
R = (Hex(hex[0]) << 4) | Hex(hex[1]);
@ -333,18 +336,25 @@ namespace MCGalaxy {
}
c.R = (byte)R; c.G = (byte)G; c.B = (byte)B; c.A = 255;
return true;
}
/// <summary> Parses an #RRGGBB hex color string. </summary>
public static ColorDesc ParseHex(string hex) {
ColorDesc c;
if (!TryParseHex(hex, out c)) throw new ArgumentException("invalid input");
return c;
}
/// <summary> Gets the index of the given hex character. </summary>
public static int Hex(char value) {
static int Hex(char value) {
if (value >= '0' && value <= '9')
return (int)(value - '0');
if (value >= 'a' && value <= 'f')
return (int)(value - 'a') + 10;
if (value >= 'A' && value <= 'F')
return (int)(value - 'A') + 10;
throw new ArgumentException("Non hex char: " + value);
return -1;
}
}

View File

@ -135,14 +135,11 @@ namespace MCGalaxy.Commands {
/// <summary> Attempts to parse the given argument as a hex color. </summary>
public static bool GetHex(Player p, string input, ref ColorDesc col) {
if (input.Length > 0 && input[0] == '#')
input = input.Substring(1);
if (!Utils.IsValidHex(input)) {
ColorDesc tmp;
if (!Colors.TryParseHex(input, out tmp)) {
Player.Message(p, "\"#{0}\" is not a valid HEX color.", input); return false;
}
col = Colors.ParseHex(input); return true;
col = tmp; return true;
}
internal static bool GetCoords(Player p, string[] args, int argsOffset, ref Vec3S32 P) {

View File

@ -174,7 +174,7 @@ namespace MCGalaxy.Commands.Info {
Color(cfg.CloudColor), Color(cfg.LightColor), Color(cfg.ShadowColor));
Player.Message(p, "Water level: &b{0}%S, Bedrock offset: &b{1}%S, Clouds height: &b{2}%S, Max fog distance: &b{3}",
cfg.EdgeLevel,cfg.SidesOffset, cfg.CloudsHeight, cfg.MaxFogDistance);
cfg.EdgeLevel, cfg.SidesOffset, cfg.CloudsHeight, cfg.MaxFogDistance);
Player.Message(p, "Edge Block: &b{0}%S, Horizon Block: &b{1}",
Block.GetName(p, cfg.EdgeBlock), Block.GetName(p, cfg.HorizonBlock));
Player.Message(p, "Clouds speed: &b{0}%%S, Weather speed: &b{1}%",

View File

@ -74,22 +74,22 @@ namespace MCGalaxy.Drawing.Ops {
PaletteEntry[] front = new PaletteEntry[Palette.Entries.Length];
PaletteEntry[] back = new PaletteEntry[Palette.Entries.Length];
ColorDesc sun = Colors.ParseHex("FFFFFF");
ColorDesc dark = Colors.ParseHex("9B9B9B");
if (Utils.IsValidHex(Level.Config.LightColor)) {
sun = Colors.ParseHex(Level.Config.LightColor);
ColorDesc sun, dark, bright;
if (!Colors.TryParseHex(Level.Config.LightColor, out sun)) {
sun = Colors.ParseHex("FFFFFF");
}
if (Utils.IsValidHex(Level.Config.ShadowColor)) {
dark = Colors.ParseHex(Level.Config.ShadowColor);
if (!Colors.TryParseHex(Level.Config.ShadowColor, out dark)) {
dark = Colors.ParseHex("9B9B9B");
}
bright = Colors.ParseHex("FFFFFF");
for (int i = 0; i < Palette.Entries.Length; i++) {
PaletteEntry entry = Palette.Entries[i];
BlockDefinition def = Level.GetBlockDef(entry.Block);
if (def != null && def.FullBright) {
front[i] = Multiply(entry, Colors.ParseHex("FFFFFF"));
back[i] = Multiply(entry, Colors.ParseHex("FFFFFF"));
front[i] = Multiply(entry, bright);
back[i] = Multiply(entry, bright);
} else {
front[i] = Multiply(entry, sun);
back[i] = Multiply(entry, dark);

View File

@ -30,7 +30,7 @@ namespace MCGalaxy {
const int posShift = 32;
const int idShift = 12;
const int blockMask = 0x7FF;
const int blockMask = (1 << 12) - 1;
public static void Loop(SchedulerTask task) {
Level[] loaded = LevelInfo.Loaded.Items;

View File

@ -55,7 +55,7 @@ namespace MCGalaxy {
public BlockID GetBlock(ushort x, ushort y, ushort z, out int index) {
if (x >= Width || y >= Height || z >= Length || blocks == null) { index = -1; return Block.Invalid; }
index = x + Width * (z + y * Length);
byte raw = blocks[x + Width * (z + y * Length)];
byte raw = blocks[index];
return raw != Block.custom_block ? raw : (BlockID)(Block.Extended | GetExtTileNoCheck(x, y, z));
}

View File

@ -114,14 +114,14 @@ namespace MCGalaxy {
MaxX == lvl.Width - 1 && MaxY == lvl.Height - 1 && MaxZ == lvl.Length - 1;
}
public bool Shows { get { return Config.ShowAlpha != 0 && Config.ShowColor != ""; } }
public bool Shows { get { return Config.ShowAlpha != 0 && Config.ShowColor.Length > 0; } }
public void Show(Player p) {
if (!p.Supports(CpeExt.SelectionCuboid) || !Shows) return;
ColorDesc col = Colors.ParseHex(Config.ShowColor);
p.Send(Packet.MakeSelection(
ID, Config.Name, new Vec3U16(MinX, MinY, MinZ),
new Vec3U16((ushort)(MaxX + 1), (ushort)(MaxY + 1), (ushort)(MaxZ + 1)),
ColorDesc col; Colors.TryParseHex(Config.ShowColor, out col);
Vec3U16 min = new Vec3U16(MinX, MinY, MinZ);
Vec3U16 max = new Vec3U16((ushort)(MaxX + 1), (ushort)(MaxY + 1), (ushort)(MaxZ + 1));
p.Send(Packet.MakeSelection(ID, Config.Name, min, max,
col.R, col.G, col.B, (byte)Config.ShowAlpha, p.hasCP437));
}

View File

@ -136,14 +136,10 @@ namespace MCGalaxy {
}
public void SendEnvColor(byte type, string hex) {
if (String.IsNullOrEmpty(hex)) {
Send(Packet.EnvColor(type, -1, -1, -1)); return;
}
try {
ColorDesc c = Colors.ParseHex(hex);
ColorDesc c;
if (Colors.TryParseHex(hex, out c)) {
Send(Packet.EnvColor(type, c.R, c.G, c.B));
} catch (ArgumentException) {
} else {
Send(Packet.EnvColor(type, -1, -1, -1));
}
}

View File

@ -26,15 +26,6 @@ namespace MCGalaxy {
/// <summary> The absolute path on disc of the folder MCGalaxy.exe is currently running from. </summary>
public static string FolderPath { get { return AppDomain.CurrentDomain.BaseDirectory; } }
public static bool IsValidHex(string hex) {
if (hex.Length != 6) return false;
for (int i = 0; i < hex.Length; i++) {
if (!Colors.IsStandard(hex[i])) return false;
}
return true;
}
public static string Hex(byte r, byte g, byte b) {
return "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
}