/// <summary> /// /// </summary> /// <param name="input"> /// /// </param> /// <returns> /// </returns> /// <exception cref="FormatException"> /// </exception> public static TwitterColor FromString(string input) { if (input.Length < 6) { input = input.PadLeft(6, '0'); } var pattern = @"^#?(?<r>[0-9A-Fa-f]{2})(?<g>[0-9A-Fa-f]{2})(?<b>[0-9A-Fa-f]{2})$"; var match = Regex.Match(input, pattern, RegexOptions.IgnoreCase); var isRgb = match.Success; if (!isRgb) { pattern = @"^#?(?<a>[0-9A-Fa-f]{2})(?<r>[0-9A-Fa-f]{2})(?<g>[0-9A-Fa-f]{2})(?<b>[0-9A-Fa-f]{2})$"; match = Regex.Match(input, pattern, RegexOptions.IgnoreCase); if (!match.Success) { throw new FormatException( "The format is incorrect, expected format: #RRGGBB, RRGGBB, " + "#AARRGGBB or AARRGGBB."); } } TwitterColor result = default(TwitterColor); result.A = isRgb ? (byte)255 : byte.Parse(match.Groups["a"].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); result.B = Byte.Parse(match.Groups["b"].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); result.G = Byte.Parse(match.Groups["g"].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); result.R = Byte.Parse(match.Groups["r"].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); return(result); }
/// <summary> /// The from argb. /// </summary> /// <param name="a"> /// The a. /// </param> /// <param name="r"> /// The r. /// </param> /// <param name="g"> /// The g. /// </param> /// <param name="b"> /// The b. /// </param> /// <returns> /// </returns> public static TwitterColor FromArgb(byte a, byte r, byte g, byte b) { TwitterColor result = default(TwitterColor); result.A = a; result.B = b; result.G = g; result.R = r; return(result); }
public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) { if (objectType != typeof(TwitterColor) && objectType != typeof(TwitterColor?)) { throw new NotSupportedException(); } if (reader.TokenType == JsonToken.Null) { if (!reader.ValueType.IsNullableType()) { throw new NotSupportedException(); } return(null); } return(TwitterColor.FromString(reader.Value.ToString())); }
/// <summary> /// Sets one or more hex values that control the color scheme of the authenticating /// user's profile page on twitter.com. /// </summary> /// <param name="backgroundColor">Profile background color.</param> /// <param name="linkColor">Profile link color.</param> /// <param name="sidebarBorderColor">Profile sidebar's border color.</param> /// <param name="sidebarFillColor">Profile sidebar's background color.</param> /// <param name="textColor">Profile text color.</param> /// <param name="includeEntities"></param> /// <param name="skipStatus"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="TwitterException"></exception> public TwitterUser UpdateProfileColors(TwitterColor? backgroundColor = null, TwitterColor? linkColor = null, TwitterColor? sidebarBorderColor = null, TwitterColor? sidebarFillColor = null, TwitterColor? textColor = null, bool includeEntities = true, bool skipStatus = false) { if (!this.TwitterApi.Authenticated) throw new InvalidOperationException("Authentication required."); var postData = new Dictionary<string, string> { { "include_entities", includeEntities ? "true" : "false" }, { "skip_status", skipStatus ? "true" : "false" } }; if (backgroundColor.HasValue) postData.Add("profile_background_color", backgroundColor.Value.ToString()); if (linkColor.HasValue) postData.Add("profile_link_color", linkColor.Value.ToString()); if (sidebarBorderColor.HasValue) postData.Add("profile_sidebar_border_color", sidebarBorderColor.Value.ToString()); if (sidebarFillColor.HasValue) postData.Add("profile_sidebar_fill_color", sidebarFillColor.Value.ToString()); if (textColor.HasValue) postData.Add("profile_text_color", textColor.Value.ToString()); var uri = new Uri(this.CommandBaseUri + "/update_profile_colors.json"); var response = this.TwitterApi.ExecuteAuthenticatedRequest(uri, HttpMethod.Post, postData); var twitterUser = TwitterObject.Parse<TwitterUser>(response); return twitterUser; }