public static bool TryGetIColorData <T>(this IDataObject data, out T[] colorData) where T : IColorData { IColorData[] colors = null; IEnumerable <object> wrappedData; if (data.TryGetWrappedData(ColorDataFormat, DataObjectStorage.Value, out wrappedData)) { colors = wrappedData.OfType <IColorData>().ToArray(); } else if (data.ContainsString()) { colors = ParseIColorData(data.GetString()); } if (colors == null) { colorData = null; return(false); } // Don't care which format? Great, just return the array as is if (typeof(T) == typeof(IColorData)) { colorData = colors.OfType <T>().ToArray(); } // Otherwise, convert to the requested color format else { colorData = colors.Select(c => c is T ? (T)c : c.ConvertTo <T>()).ToArray(); } return(colorData.Any()); }
public static bool ContainsIColorData(this IDataObject data) { if (data.GetWrappedDataPresent(typeof(IColorData[]))) { return(true); } if (data.ContainsString()) { string valString = data.GetString(); string[] token = valString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); byte[] valToken = new byte[4]; valToken[3] = 255; bool success = true; for (int i = 0; i < token.Length; i++) { token[i] = token[i].Trim(); if (!byte.TryParse(token[i], out valToken[i])) { success = false; break; } } if (success) { return(true); } } return(false); }
public static T[] GetIColorData <T>(this IDataObject data) where T : IColorData { IColorData[] clrArray = null; if (data.GetWrappedDataPresent(typeof(IColorData[]))) { clrArray = data.GetWrappedData(typeof(IColorData[])) as IColorData[]; } else if (data.ContainsString()) { string valString = data.GetString(); string[] token = valString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); byte[] valToken = new byte[4]; valToken[3] = 255; bool success = true; for (int i = 0; i < token.Length; i++) { token[i] = token[i].Trim(); if (!byte.TryParse(token[i], out valToken[i])) { success = false; break; } } if (success) { clrArray = new IColorData[] { new ColorRgba(valToken[0], valToken[1], valToken[2], valToken[3]) } } ; } if (clrArray != null) { // Don't care which format? Great, just return the array as is if (typeof(T) == typeof(IColorData)) { return((T[])(object)clrArray); } // Convert to specific format return(clrArray.Select <IColorData, T>(ic => ic is T ? (T)ic : ic.ConvertTo <T>()).ToArray()); } else { return(null); } }
public static bool ContainsIColorData(this IDataObject data) { if (data.GetWrappedDataPresent(ColorDataFormat, DataObjectStorage.Value)) { return(true); } if (data.ContainsString()) { IColorData[] clrArray; if (TryParseIColorData(data.GetString(), out clrArray)) { return(true); } } return(false); }