1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-16 12:23:06 +08:00

Use Span for OsuColour.FromHex

This commit is contained in:
Berkan Diler 2020-02-14 03:19:25 +01:00
parent c1924535ff
commit 50899ddccb

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Globalization;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osuTK.Graphics; using osuTK.Graphics;
@ -14,41 +15,40 @@ namespace osu.Game.Graphics
public static Color4 FromHex(string hex) public static Color4 FromHex(string hex)
{ {
if (hex[0] == '#') var hexSpan = hex[0] == '#' ? hex.AsSpan().Slice(1) : hex.AsSpan();
hex = hex.Substring(1);
switch (hex.Length) switch (hexSpan.Length)
{ {
default: default:
throw new ArgumentException(@"Invalid hex string length!"); throw new ArgumentException(@"Invalid hex string length!");
case 3: case 3:
return new Color4( return new Color4(
(byte)(Convert.ToByte(hex.Substring(0, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(0, 1), NumberStyles.HexNumber) * 17),
(byte)(Convert.ToByte(hex.Substring(1, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(1, 1), NumberStyles.HexNumber) * 17),
(byte)(Convert.ToByte(hex.Substring(2, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(2, 1), NumberStyles.HexNumber) * 17),
255); 255);
case 6: case 6:
return new Color4( return new Color4(
Convert.ToByte(hex.Substring(0, 2), 16), byte.Parse(hexSpan.Slice(0, 2), NumberStyles.HexNumber),
Convert.ToByte(hex.Substring(2, 2), 16), byte.Parse(hexSpan.Slice(2, 2), NumberStyles.HexNumber),
Convert.ToByte(hex.Substring(4, 2), 16), byte.Parse(hexSpan.Slice(4, 2), NumberStyles.HexNumber),
255); 255);
case 4: case 4:
return new Color4( return new Color4(
(byte)(Convert.ToByte(hex.Substring(0, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(0, 1), NumberStyles.HexNumber) * 17),
(byte)(Convert.ToByte(hex.Substring(1, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(1, 1), NumberStyles.HexNumber) * 17),
(byte)(Convert.ToByte(hex.Substring(2, 1), 16) * 17), (byte)(byte.Parse(hexSpan.Slice(0, 1), NumberStyles.HexNumber) * 17),
(byte)(Convert.ToByte(hex.Substring(3, 1), 16) * 17)); (byte)(byte.Parse(hexSpan.Slice(0, 1), NumberStyles.HexNumber) * 17));
case 8: case 8:
return new Color4( return new Color4(
Convert.ToByte(hex.Substring(0, 2), 16), byte.Parse(hexSpan.Slice(0, 2), NumberStyles.HexNumber),
Convert.ToByte(hex.Substring(2, 2), 16), byte.Parse(hexSpan.Slice(2, 2), NumberStyles.HexNumber),
Convert.ToByte(hex.Substring(4, 2), 16), byte.Parse(hexSpan.Slice(4, 2), NumberStyles.HexNumber),
Convert.ToByte(hex.Substring(6, 2), 16)); byte.Parse(hexSpan.Slice(6, 2), NumberStyles.HexNumber));
} }
} }