Browse Source

Optimize ColorBuilder

pull/567/head
Jason Nelson 8 years ago
parent
commit
fda2d1fc04
  1. 21
      src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs

21
src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers.Binary;
using System.Globalization; using System.Globalization;
namespace SixLabors.ImageSharp.PixelFormats namespace SixLabors.ImageSharp.PixelFormats
@ -26,18 +27,14 @@ namespace SixLabors.ImageSharp.PixelFormats
Guard.NotNullOrWhiteSpace(hex, nameof(hex)); Guard.NotNullOrWhiteSpace(hex, nameof(hex));
hex = ToRgbaHex(hex); hex = ToRgbaHex(hex);
uint packedValue;
if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out packedValue)) if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint packedValue))
{ {
throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex)); throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex));
} }
TPixel result = default; TPixel result = default;
var rgba = new Rgba32( var rgba = new Rgba32(BinaryPrimitives.ReverseEndianness(packedValue));
(byte)(packedValue >> 24),
(byte)(packedValue >> 16),
(byte)(packedValue >> 8),
(byte)(packedValue >> 0));
result.PackFromRgba32(rgba); result.PackFromRgba32(rgba);
return result; return result;
@ -96,12 +93,12 @@ namespace SixLabors.ImageSharp.PixelFormats
return null; return null;
} }
string red = char.ToString(hex[0]); char r = hex[0];
string green = char.ToString(hex[1]); char g = hex[1];
string blue = char.ToString(hex[2]); char b = hex[2];
string alpha = hex.Length == 3 ? "F" : char.ToString(hex[3]); char a = hex.Length == 3 ? 'F' : hex[3];
return string.Concat(red, red, green, green, blue, blue, alpha, alpha); return new string(new[] { r, r, g, g, b, b, a, a });
} }
} }
} }
Loading…
Cancel
Save