예제 #1
0
        /// <summary>
        /// Gets a specific color related to the current workspace.
        /// </summary>
        /// <param name="what">Type of the color to be returned.</param>
        /// <returns>The desired color.</returns>
        public Color GetWorkspaceColor(Colors what)
        {
            EnsureWorkspaceHasColor( );             // Assing the color, if needeed

            // Read the base color (or take the default for Default workspace)
            Color colorBase = _workspace != null?Color.FromArgb(_workspace.GetIntProp(((WorkspaceManager)Core.WorkspaceManager).Props.WorkspaceColor)) : SystemColors.Control;

            if (what == Colors.Base)
            {
                return(colorBase);                // The base workspace color, as is
            }
            // Get the HLS components
            byte bH, bL, bS;

            ColorManagement.RGBtoHLS(ColorManagement.RGB(colorBase), out bH, out bL, out bS);
            double H = bH;
            double L = bL;
            double S = bS;

            // Produce the color appropriate
            switch (what)
            {
            case Colors.Light:
                L += 27.0 * 1.5;
                S -= 43.0 * 1.5;
                break;

            case Colors.Dark:
                L -= 16.0 * 2.0;
                S -= 8.0 * 2.0;
                break;

            case Colors.Shadow:
                L -= 8.0 * 2.0;
                S -= 75.0 * 2.0;
                break;

            default:
                throw new InvalidOperationException("Unsupported workspace color type.");
            }

            // Constraint
            bH = (byte)(H >= 0 ? (H <= ColorManagement.MaxHLS - 1 ? H : ColorManagement.MaxHLS - 1) : 0);
            bL = (byte)(L >= 0 ? (L <= ColorManagement.MaxHLS - 1 ? L : ColorManagement.MaxHLS - 1) : 0);
            bS = (byte)(S >= 0 ? (S <= ColorManagement.MaxHLS - 1 ? S : ColorManagement.MaxHLS - 1) : 0);

            // Produce the color
            return(ColorManagement.HLStoRGB(bH, bL, bS));
        }
예제 #2
0
        /// <summary>
        /// Checks whether the given workspace has a color assigned to it, and, if no, generates a random one.
        /// For the <c>Null</c> Default workspace, does nothing.
        /// </summary>
        public void EnsureWorkspaceHasColor( )
        {
            if (_workspace == null)
            {
                return;                                                    // No checks for the default workspace
            }
            WorkspaceManager wm = (WorkspaceManager)Core.WorkspaceManager; // Provides IDs of workspace resource properties

            if (_workspace.HasProp(wm.Props.WorkspaceColor))
            {
                return;                 // Assigned already, do nothing
            }
            // No, the workspace does not have a color yet
            // Choose one based on its title
            byte  hue   = (byte)new Random(_workspace.DisplayName.GetHashCode()).Next(ColorManagement.MaxHLS);
            Color color = ColorManagement.HLStoRGB(hue, DefaultWorkspaceLuminocity, DefaultWorkspaceSaturation);

            // Apply the property synchronously so that it would be valid as we exit the method
            new ResourceProxy(_workspace).SetProp(wm.Props.WorkspaceColor, color.ToArgb());
        }