Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4382180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:44:44+00:00 2026-05-21T12:44:44+00:00

Recently I came across a .NET color chart based on their hue and brightness

  • 0

Recently I came across a .NET color chart based on their hue and brightness value. What stroke me is the crazy grayscale chart. For example, DarkGray is actually lighter then Gray ? Also, I can’t see any logic in the gradation of rgb values, it goes from 0 to 105 to 128 ?

0   : Black
105 : DimGray 
128 : Gray
169 : DarkGray!
192 : Silver
211 : LightGray 
220 : Gainsboro
245 : Ghostwhite
255 : White

http://sites.google.com/site/cdeveloperresources/

color chart - see link above

What I want is a GrayScaleBrushes class which behaves exactly like the Brushes class, but with my custom scheme, like :

GrayScaleBrushes.Pct05
GrayScaleBrushes.Pct10
GrayScaleBrushes.Pct15
..all the way to.Pct95
...
ie: e.FillRectangle( GrayScaleBrushes.Pct05, exampleRect );

How to do that, making sure that the brushes will dispose correctly ?

Edit: The .NET Brushes class looks like the following (disassembled using reflector ).

public sealed class Brushes
{
    // Fields
    private static readonly object AliceBlueKey = new object();

    // Methods
    private Brushes()
    {
    }

    // Properties
    public static Brush AliceBlue
    {
        get
        {
            Brush brush = (Brush) SafeNativeMethods.Gdip.ThreadData[AliceBlueKey];
            if (brush == null)
            {
                brush = new SolidBrush(Color.AliceBlue);
                SafeNativeMethods.Gdip.ThreadData[AliceBlueKey] = brush;
            }
            return brush;
        }
    }
}

SafeNativeMethods seems unaccessible to me. Suppose I just returned a SolidBrush in a static method, would that make everything dispose correctly ? (And how to test that?)

public sealed class GrayScaleBrushes
{
    private static SolidBrush pct05 = null;

    public static SolidBrush Pct05
    {
        get
        {
            if (pct05 == null)
            {
                int rgbVal = GetRgbValFromPct( 5 );
                pct05 = new SolidBrush(Color.FromArgb(rgbVal, rgbVal, rgbVal));
            }
            return pct05;
        }
    }

    private static int GetRgbValFromPct(int pct)
    {
        return 255 - (int)(((float)pct / 100f) * 255f);
    }
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:44:45+00:00Added an answer on May 21, 2026 at 12:44 pm

    The SafeNativeMethods is in this case just a simple cache that holds a copy of the brush. So a second call to the property getter won’t create a new instance. Instead it will always return the same brush.

    To accomplish this you could rewrite your function maybe like this:

    public static class GrayScaleBrushes
    {
        private static SolidBrush _Pct05;
    
        public static SolidBrush Pct05
        {
            get
            {
                if (_Pct05 == null)
                {
                    var value = GetRgbValFromPct(5);
                    _Pct05 = new SolidBrush(Color.FromArgb(value, value, value));
                }
    
                return _Pct05;
            }
        }
    
        private static int GetRgbValFromPct(int pct)
        {
            // no need to convert to float and back to int again
            return 255 - ((pct * 255) / 100);
        }
    }
    

    This solution will create the gray scales as needed but by the costs of checking for the null every time it is called. You can accomplish this speed problem by changing it again a memory problem by taking an approach like this:

    public static class GrayScaleBrushes
    {
        public static readonly SolidBrush Pct05;
    
        static GrayScaleBrushes()
        {
            var value = GetRgbValFromPct(5);
            Pct05 = new SolidBrush(Color.FromArgb(value, value, value));
        }
    }
    

    But i think in this case it just a matter of taste, cause nor the speed nor the memory will be a real problem in both cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across a way to develop pluggable application modules when using ASP.NET
I came across this comprehensive explanation of the new .NET TPL library recently, and
Recently in a previous project I came across a peculiar difference between VB.NET and
Working on my first ASP.Net MVC2 web app recently, I came across some issues
I recently came across Remote Validation in asp.net mvc . Really helpful feature but
I came across an interesting problem recently. In an ASP.NET Master Page, I have
I recently came across a kind of dispatch pattern based on the concepts of
What is scchema importer Extension class in .net webservice.Recently i came across this class
I recently came across this interesting term and searched on Net to know more
I recently came across the following methods. I tried googling and did an example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.