private static async Task <DiscordColor?> GetImageColorAsync(string url, DiscordColor defaultColor) { try { if (string.IsNullOrEmpty(url)) { return(null); } using (var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false)) using (var memStream = new MemoryStream()) { await imgStream.CopyToAsync(memStream).ConfigureAwait(false); // minimum jpg size is 119 bytes, png is 67 bytes if (memStream.Length < 64) { return(null); } memStream.Seek(0, SeekOrigin.Begin); return(ColorGetter.Analyze(memStream.ToArray(), defaultColor)); } } catch (Exception e) { Config.Log.Warn(e); } return(null); }
public Button ( BitGetters.BitContent content, Func <ClickArgs, Task> onClick, ColorGetter?color = null, bool isStatic = false, BitGetters.BitIsVisible?isVisible = null, BitGetters.BitIsDisabled?isDisabled = null, BitGetters.BitClasses?classes = null, BitGetters.BitSize?margin = null, BitGetters.BitSize?padding = null, BitGetters.BitColor?foregroundColor = null, BitGetters.BitColor?backgroundColor = null, BitGetters.BitPixels?pixelsHeight = null, BitGetters.BitPixels?pixelsWidth = null, BitGetters.BitREM?fontSize = null, BitGetters.BitWeight?fontWeight = null, BitGetters.BitDisplay?display = null, BitGetters.BitIsHighlighted?isHighlighted = null, BitGetters.BitData?data = null, BitGetters.BitTooltip?tooltip = null ) { Spec = new BitSpec { Content = content, IsStatic = isStatic, IsVisible = isVisible, IsDisabled = isDisabled, Classes = classes, Margin = margin, Padding = padding, ForegroundColor = foregroundColor, BackgroundColor = backgroundColor, PixelsHeight = pixelsHeight, PixelsWidth = pixelsWidth, FontSize = fontSize, FontWeight = fontWeight, Display = display, IsHighlighted = isHighlighted, Data = data, Tooltip = tooltip, }; _onClick = onClick; _color = color ?? DefaultColorGetter; ConstantClasses = new ClassSet( "Integrant.Element.Override.Button", "Integrant.Element.Bit", "Integrant.Element.Bit." + nameof(Button) ); // ConstantClasses.Add("Integrant.Element.Override.Button:" + color.inv); Cache(additionalClasses: LocalClasses()); }
public static async Task <(string url, DiscordColor color)> GetThumbnailUrlWithColorAsync(DiscordClient client, string contentId, DiscordColor defaultColor, string url = null) { if (string.IsNullOrEmpty(contentId)) { throw new ArgumentException("ContentID can't be empty", nameof(contentId)); } contentId = contentId.ToUpperInvariant(); using (var db = new ThumbnailDb()) { var info = await db.TitleInfo.FirstOrDefaultAsync(ti => ti.ContentId == contentId, Config.Cts.Token).ConfigureAwait(false); if (info == null) { info = new TitleInfo { ContentId = contentId, ThumbnailUrl = url, Timestamp = DateTime.UtcNow.Ticks }; var thumb = await db.Thumbnail.FirstOrDefaultAsync(t => t.ContentId == contentId).ConfigureAwait(false); if (thumb?.EmbeddableUrl is string eUrl && thumb.Url is string thumbUrl && thumbUrl == url) { info.ThumbnailEmbeddableUrl = eUrl; } info = db.TitleInfo.Add(info).Entity; await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); } if (string.IsNullOrEmpty(info.ThumbnailEmbeddableUrl)) { var em = await GetEmbeddableUrlAsync(client, contentId, info.ThumbnailUrl).ConfigureAwait(false); if (em.url is string eUrl) { info.ThumbnailEmbeddableUrl = eUrl; if (em.image is byte[] jpg) { info.EmbedColor = ColorGetter.Analyze(jpg, defaultColor).Value; } await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); } } if (!info.EmbedColor.HasValue) { var c = await GetImageColorAsync(info.ThumbnailEmbeddableUrl, defaultColor).ConfigureAwait(false); if (c.HasValue) { info.EmbedColor = c.Value.Value; await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); } } var color = info.EmbedColor.HasValue ? new DiscordColor(info.EmbedColor.Value) : defaultColor; return(info.ThumbnailEmbeddableUrl, color); } }
public static async Task <(string url, DiscordColor color)> GetThumbnailUrlWithColorAsync(DiscordClient client, string contentId, DiscordColor defaultColor, string url = null) { if (string.IsNullOrEmpty(contentId)) { throw new ArgumentException("ContentID can't be empty", nameof(contentId)); } contentId = contentId.ToUpperInvariant(); using var db = new ThumbnailDb(); var info = await db.Thumbnail.FirstOrDefaultAsync(ti => ti.ContentId == contentId, Config.Cts.Token).ConfigureAwait(false); info ??= new Thumbnail { Url = url }; if (info.Url == null) { return(null, defaultColor); } DiscordColor?analyzedColor = null; if (string.IsNullOrEmpty(info.EmbeddableUrl)) { var em = await GetEmbeddableUrlAsync(client, contentId, info.Url).ConfigureAwait(false); if (em.url is string eUrl) { info.EmbeddableUrl = eUrl; if (em.image is byte[] jpg) { Config.Log.Trace("Getting dominant color for " + eUrl); analyzedColor = ColorGetter.Analyze(jpg, defaultColor); var c = analyzedColor.Value.Value; if (c != defaultColor.Value) { info.EmbedColor = c; } } await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); } } if ((!info.EmbedColor.HasValue && !analyzedColor.HasValue) || (info.EmbedColor.HasValue && info.EmbedColor.Value == defaultColor.Value)) { var c = await GetImageColorAsync(info.EmbeddableUrl, defaultColor).ConfigureAwait(false); if (c.HasValue && c.Value.Value != defaultColor.Value) { info.EmbedColor = c.Value.Value; await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); } } var color = info.EmbedColor.HasValue ? new DiscordColor(info.EmbedColor.Value) : defaultColor; return(info.EmbeddableUrl, color); }
public static async Task <DiscordColor?> GetImageColorAsync(string url, DiscordColor?defaultColor = null) { try { if (string.IsNullOrEmpty(url)) { return(null); } if (ColorCache.TryGetValue(url, out DiscordColor? result)) { return(result); } using var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false); using var memStream = Config.MemoryStreamManager.GetStream(); await imgStream.CopyToAsync(memStream).ConfigureAwait(false); // minimum jpg size is 119 bytes, png is 67 bytes if (memStream.Length < 64) { return(null); } memStream.Seek(0, SeekOrigin.Begin); Config.Log.Trace("Getting dominant color for " + url); result = ColorGetter.Analyze(memStream.ToArray(), defaultColor); ColorCache.Set(url, result, TimeSpan.FromHours(1)); return(result); } catch (Exception e) { Config.Log.Warn(e); } return(null); }
/// <summary> /// The idea here is to cache descriptors so that we have lightning fast performance. /// </summary> /// <param name="parent">The ScriptEngine that owns this ColorInstance.</param> private static void PopulateDescriptors(ScriptEngine parent) { if (_descriptors != null) return; _descriptors = new PropertyDescriptor[5]; for (int i = 0; i < 4; ++i) { var getter = new ColorGetter(parent, i); var setter = new ColorSetter(parent, i); _descriptors[i] = new PropertyDescriptor(getter, setter, PropertyAttributes.Sealed); } _descriptors[4] = new PropertyDescriptor(new ToStringFunc(parent, "color"), PropertyAttributes.Sealed); }