Skip to content

Instantly share code, notes, and snippets.

@jddcef
Created March 2, 2012 05:33
Show Gist options
  • Save jddcef/1955934 to your computer and use it in GitHub Desktop.
Save jddcef/1955934 to your computer and use it in GitHub Desktop.
ClientColors.cs implementation in mxit sdk inherits IClientColors
/*
Software Copyright Notice
 
Copyright © 2004-2010 MXit Lifestyle Development Company (Pty) Ltd.
All rights are reserved
 
Copyright exists in this computer program and it is protected by
copyright law and by international treaties. The unauthorised use,
reproduction or distribution of this computer program constitute
acts of copyright infringement and may result in civil and criminal
penalties. Any infringement will be prosecuted to the maximum extent
possible.
 
MXit Lifestyle Development Company (Pty) Ltd chooses the following
address for delivery of all legal proceedings and notices:
  Riesling House,
  Brandwacht Office Park,
  Trumali Road,
  Stellenbosch,
  7600,
  South Africa.
 
The following computer programs, or portions thereof, are used in
this computer program under licenses obtained from third parties.
You are obliged to familiarise yourself with the contents of those
licenses.
 
Third-party software used:
*/
using System.Collections.Generic;
using System.Drawing;
using System.Text;
 
using MXit.User;
 
namespace MXit.Messaging.MessageElements
{
    /// <summary>
    /// A list of colors, for user interface elements, within a MXit client.
    /// </summary>
    internal class ClientColors : IClientColors
    {
        /// <summary>
        /// Client color dictionary.
        /// </summary>
        private readonly Dictionary<ClientColorType, Color> _colors = new Dictionary<ClientColorType, Color>();
 
        /// <summary>
        /// Add / retrieve a color to / from the list of client colors.
        /// </summary>
        /// <param name="type">Client color type to add / retrieve.</param>
        /// <returns>Color of the client color type.</returns>
        public Color this[ClientColorType type]
        {
            get {
                return _colors[type];
            }
            set
            {
                _colors[type] = value;
            }
        }
 
        /// <summary>
        /// Test if there is a color associated with a given color type.
        /// </summary>
        /// <param name="type">Client color type.</param>
        /// <returns>True if a color is set for the given color type.</returns>
        public bool Contains(ClientColorType type)
        {
            return _colors.ContainsKey(type);
        }
 
        /// <summary>
        /// Encode the element, i.e. create the underlying message body text for the element.
        /// </summary>
        /// <returns>Encoded element text.</returns>
        public string Encode()
        {
            StringBuilder field = new StringBuilder();
            foreach (KeyValuePair<ClientColorType, Color> pair in _colors)
            {
                if (field.Length != 0)
                {
                    field.Append(";");
                }
 
                field.Append((int)pair.Key);
                field.Append(",");
                // a full transparent color (except 0 which is special) does not make sense, so
                // assume a fully opague color is meant
                uint col = (uint)pair.Value.ToArgb();
                if (col != 0 && (col & 0xff000000) == 0)
                {
                    col |= 0xff000000;
                }
                field.Append(col.ToString("X8"));
            }
            return field.ToString();
        }
 
        #region Constructor
 
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientColors"/> class.
        /// </summary>
        public ClientColors()
        {
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientColors"/> class.
        /// </summary>
        /// <param name="type">Client color type (for the first type in the list).</param>
        /// <param name="color">Color to associate with the type (for the first type in the list).</param>
        public ClientColors(ClientColorType type, Color color)
        {
            _colors[type] = color;
        }
        #endregion
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment