/// <summary>Parse JSON string and return token arguments.</summary> public static RecolorTokenArguments Parse(string input) { if (string.IsNullOrWhiteSpace(input)) { throw new ArgumentException("Argument list required"); } string[] tempInput = input.Split(','); if (tempInput.Length < 5) { throw new ArgumentException($"Wrong number of items in argument list, at least 5 required, {tempInput.Length} found"); } return(new RecolorTokenArguments { ContentPackName = tempInput[0].Trim(), AssetName = tempInput[1].Trim(), SourcePath = tempInput[2].Trim(), MaskPath = tempInput[3].Trim(), BlendColor = Utility.ColorFromHtml(tempInput[4].Trim()), DesaturationMode = tempInput.Length > 5 ? Desaturation.ParseEnum(tempInput[5].Trim()) : Desaturation.Mode.None }); }
/// <summary>Parse JSON string and return token arguments.</summary> public static RecolorTokenArguments Parse(string input) { if (string.IsNullOrWhiteSpace(input)) { throw new ArgumentException("Argument list required"); } string[] tempInput = input.Split(','); if (tempInput.Length < 5) { throw new ArgumentException($"Wrong number of items in argument list, at least 5 required, {tempInput.Length} found"); } return(new RecolorTokenArguments { ContentPackName = tempInput[0].Trim(), AssetName = tempInput[1].Trim(), SourcePath = tempInput[2].Trim(), MaskPath = tempInput[3].Trim(), BlendColor = Utility.ColorFromHtml(tempInput[4].Trim()), DesaturationMode = tempInput.Length > 5 ? Desaturation.ParseEnum(tempInput[5].Trim()) : Desaturation.Mode.None, FlipMode = tempInput.Length > 6 ? Flip.ParseEnum(tempInput[6].Trim()) : Flip.Mode.None, Brightness = tempInput.Length > 7 ? float.Parse(tempInput[7].Trim(), System.Globalization.CultureInfo.InvariantCulture) : 1.0f }); }
/// <summary>Extracts a sub image using the given mask and brightness.</summary> private Texture2D ExtractSubImage(Texture2D source, Texture2D mask, Desaturation.Mode desaturationMode, float brightness) { // Size checks. if (mask != null) { if (source.Width != mask.Width) { throw new ArgumentException($"Widths of image and mask don't match: {source.Width} != {mask.Width}"); } if (source.Height < mask.Height) { monitor_.Log($"Height of the image is less than height of the mask: {source.Height} < {mask.Height}", LogLevel.Info); } else if (source.Height > mask.Height) { monitor_.Log($"Height of the image is greater than height of the mask. Parts of the image not covered by mask won't be recolored: {source.Height} > {mask.Height}", LogLevel.Warn); } } if (brightness < 0.0f) { throw new ArgumentException("Brightness must not be negative"); } Color[] sourcePixels = Utility.TextureToArray(source); Color[] maskPixels = mask != null?Utility.TextureToArray(mask) : null; Color[] extractedPixels = new Color[source.Width * source.Height]; // If mask is null recolor the whole image. var getMaskValue = maskPixels == null ? (Func <Color[], int, byte>)((p, i) => (byte)0xFF) : GetMaskValue; for (int i = 0; i < sourcePixels.Length; i++) { Color pixel = Desaturation.Desaturate(sourcePixels[i], desaturationMode); byte maskValue = getMaskValue(maskPixels, i); // Multiplication is all we need: If maskValue is zero the resulting pixel is zero (TransparentBlack). // Clamping is done automatically on assignment. extractedPixels[i] = pixel * (maskValue / 255.0f) * brightness; } return(Utility.ArrayToTexture(extractedPixels, source.Width, source.Height)); }
/// <summary>Extracts a sub image using the given mask.</summary> private Texture2D ExtractSubImage(Texture2D source, Texture2D mask, Desaturation.Mode desaturationMode) { if (mask.Width != source.Width || mask.Height != source.Height) { throw new ArgumentException("Sizes of image and mask don't match"); } Color[] sourcePixels = Utility.TextureToArray(source); Color[] maskPixels = Utility.TextureToArray(mask); Color[] extractedPixels = new Color[source.Width * source.Height]; for (int i = 0; i < sourcePixels.Length; i++) { Color pixel = Desaturation.Desaturate(sourcePixels[i], desaturationMode); // Treat mask as grayscale (luma). byte maskValue = Desaturation.Desaturate(maskPixels[i], Desaturation.Mode.DesaturateLuma).R; // Multiplication is all we need: If maskValue is zero the resulting pixel is zero (TransparentBlack). extractedPixels[i] = pixel * (maskValue / 255.0f); } return(Utility.ArrayToTexture(extractedPixels, source.Width, source.Height)); }
/// <summary> /// Returns the mask value at the given index. /// This is either the grayscale value (luma) or zero if index is too big. /// </summary> private static byte GetMaskValue(Color[] maskPixels, int index) { // Treat mask as grayscale (luma). // Mask might be smaller than image. Return zero, we don't want to recolor the unmasked part of the image. return(Desaturation.Desaturate(maskPixels.ElementAtOrDefault(index), Desaturation.Mode.DesaturateLuma).R); }