Color spectrum algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil West

    #1

    Color spectrum algorithm

    Earlier this week I was tasked with displaying some data from an excel
    spreadsheet, the details of which aren't important. I decided to display the
    range of values with a cold-to-hot color model which would then be used to
    color objects in Adobe InDesign via scripting. Again, none of this is
    important, ignore the software names, this is just an introduction.

    Since I have a deep dislike of number-crunching, I didn't want to figure out
    what my number ranges should be and assign them to pre-defined colors....like
    dark blue represents 1200-1600 widgets, and green represents 2300-2700
    widgets, etc. Boring. Tedious. Yuck. Instead I wanted to match a given value
    to any point on the cold-to-hot spectrum. In other words, an analog color
    scheme, with full blue at 0%, full red at 100% and all the colors in between.

    However, in the RGB color model there's no single value that you can
    increase proportionally to the percentage increase. For example, if VB had
    the HSB model (Hue, Saturation, Brightness), I could simply increase the Hue
    value to slide through the spectrum. With RGB you have to alternately
    increase and decrease the channels' values to get from a to b, or in this
    case B to R :)

    By a happy coincidence, the number of steps it takes to get from B to R
    coincides with the quarter percentages...2 5%, 50%, 75% and 100%. So:

    0% - B
    25% - GB
    50% - G
    75% - RG
    100% - R

    where each of the letters above represents the color channel at the full
    255. So to get from 0 to 25% you have to increase the green, but to get from
    25 to 50% you have to decrease the blue, and so on.

    Here’s my code:

    Private Function ColorValue(ByVa l percent As Double) As Color
    Dim R, G, B As Byte

    Select Case True
    Case percent < 25
    R = 0
    G = percent / 25 * 255
    B = 255
    Case percent < 50
    R = 0
    G = 255
    B = (50 - percent) / 50 * 255
    Case percent < 75
    R = percent / 75 * 255
    G = 255
    B = 0
    Case percent < 100
    R = 255
    G = (100 - percent) / 100 * 255
    B = 0
    End Select

    ColorValue = Color.FromArgb( R, G, B)

    End Function

    It works perfectly well, but I wondered if anyone had an alternative
    approach. For some reason it just doesn’t feel satisfying and I have this
    nagging feeling that there’s a more elegant or clever method that I’m not
    thinking of or that I’m simply not aware of. And for the sake of my own
    education I’d like to hear any new ideas.

    Thanks. Sorry for the length of the post.

  • hoppy

    #2
    Re: Color spectrum algorithm

    [color=blue]
    > However, in the RGB color model there's no single value that you can
    > increase proportionally to the percentage increase. For example, if VB
    > had the HSB model (Hue, Saturation, Brightness), I could simply
    > increase the Hue value to slide through the spectrum. With RGB you
    > have to alternately increase and decrease the channels' values to get
    > from a to b, or in this case B to R :)[/color]

    hsb code:

    &lngWId=10

    Comment

    • Bob Powell [MVP]

      #3
      Re: Color spectrum algorithm

      There is HSB code inthe GDI+ FAQ.

      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Ramuseco Limited .NET consulting


      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.





      "Neil West" <NeilWest@discu ssions.microsof t.com> wrote in message
      news:34F7F98B-71F6-4DF3-9310-095FA74F6B89@mi crosoft.com...[color=blue]
      > Earlier this week I was tasked with displaying some data from an excel
      > spreadsheet, the details of which aren't important. I decided to display
      > the
      > range of values with a cold-to-hot color model which would then be used to
      > color objects in Adobe InDesign via scripting. Again, none of this is
      > important, ignore the software names, this is just an introduction.
      >
      > Since I have a deep dislike of number-crunching, I didn't want to figure
      > out
      > what my number ranges should be and assign them to pre-defined
      > colors....like
      > dark blue represents 1200-1600 widgets, and green represents 2300-2700
      > widgets, etc. Boring. Tedious. Yuck. Instead I wanted to match a given
      > value
      > to any point on the cold-to-hot spectrum. In other words, an analog color
      > scheme, with full blue at 0%, full red at 100% and all the colors in
      > between.
      >
      > However, in the RGB color model there's no single value that you can
      > increase proportionally to the percentage increase. For example, if VB had
      > the HSB model (Hue, Saturation, Brightness), I could simply increase the
      > Hue
      > value to slide through the spectrum. With RGB you have to alternately
      > increase and decrease the channels' values to get from a to b, or in this
      > case B to R :)
      >
      > By a happy coincidence, the number of steps it takes to get from B to R
      > coincides with the quarter percentages...2 5%, 50%, 75% and 100%. So:
      >
      > 0% - B
      > 25% - GB
      > 50% - G
      > 75% - RG
      > 100% - R
      >
      > where each of the letters above represents the color channel at the full
      > 255. So to get from 0 to 25% you have to increase the green, but to get
      > from
      > 25 to 50% you have to decrease the blue, and so on.
      >
      > Here's my code:
      >
      > Private Function ColorValue(ByVa l percent As Double) As Color
      > Dim R, G, B As Byte
      >
      > Select Case True
      > Case percent < 25
      > R = 0
      > G = percent / 25 * 255
      > B = 255
      > Case percent < 50
      > R = 0
      > G = 255
      > B = (50 - percent) / 50 * 255
      > Case percent < 75
      > R = percent / 75 * 255
      > G = 255
      > B = 0
      > Case percent < 100
      > R = 255
      > G = (100 - percent) / 100 * 255
      > B = 0
      > End Select
      >
      > ColorValue = Color.FromArgb( R, G, B)
      >
      > End Function
      >
      > It works perfectly well, but I wondered if anyone had an alternative
      > approach. For some reason it just doesn't feel satisfying and I have this
      > nagging feeling that there's a more elegant or clever method that I'm not
      > thinking of or that I'm simply not aware of. And for the sake of my own
      > education I'd like to hear any new ideas.
      >
      > Thanks. Sorry for the length of the post.
      >[/color]


      Comment

      • Bob Powell [MVP]

        #4
        Re: Color spectrum algorithm

        Another thing you might wish to do is to create a 1 bit high bitmap, draw a
        colour spectrum across it using a gradient and then pick a colour using the
        X coordinate as an index. I've done this before with a gradient brush that
        used a ColorBlend to create a wide rainbow of shades.

        --
        Bob Powell [MVP]
        Visual C#, System.Drawing

        Ramuseco Limited .NET consulting


        Find great Windows Forms articles in Windows Forms Tips and Tricks


        Answer those GDI+ questions with the GDI+ FAQ


        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.





        "Neil West" <NeilWest@discu ssions.microsof t.com> wrote in message
        news:34F7F98B-71F6-4DF3-9310-095FA74F6B89@mi crosoft.com...[color=blue]
        > Earlier this week I was tasked with displaying some data from an excel
        > spreadsheet, the details of which aren't important. I decided to display
        > the
        > range of values with a cold-to-hot color model which would then be used to
        > color objects in Adobe InDesign via scripting. Again, none of this is
        > important, ignore the software names, this is just an introduction.
        >
        > Since I have a deep dislike of number-crunching, I didn't want to figure
        > out
        > what my number ranges should be and assign them to pre-defined
        > colors....like
        > dark blue represents 1200-1600 widgets, and green represents 2300-2700
        > widgets, etc. Boring. Tedious. Yuck. Instead I wanted to match a given
        > value
        > to any point on the cold-to-hot spectrum. In other words, an analog color
        > scheme, with full blue at 0%, full red at 100% and all the colors in
        > between.
        >
        > However, in the RGB color model there's no single value that you can
        > increase proportionally to the percentage increase. For example, if VB had
        > the HSB model (Hue, Saturation, Brightness), I could simply increase the
        > Hue
        > value to slide through the spectrum. With RGB you have to alternately
        > increase and decrease the channels' values to get from a to b, or in this
        > case B to R :)
        >
        > By a happy coincidence, the number of steps it takes to get from B to R
        > coincides with the quarter percentages...2 5%, 50%, 75% and 100%. So:
        >
        > 0% - B
        > 25% - GB
        > 50% - G
        > 75% - RG
        > 100% - R
        >
        > where each of the letters above represents the color channel at the full
        > 255. So to get from 0 to 25% you have to increase the green, but to get
        > from
        > 25 to 50% you have to decrease the blue, and so on.
        >
        > Here's my code:
        >
        > Private Function ColorValue(ByVa l percent As Double) As Color
        > Dim R, G, B As Byte
        >
        > Select Case True
        > Case percent < 25
        > R = 0
        > G = percent / 25 * 255
        > B = 255
        > Case percent < 50
        > R = 0
        > G = 255
        > B = (50 - percent) / 50 * 255
        > Case percent < 75
        > R = percent / 75 * 255
        > G = 255
        > B = 0
        > Case percent < 100
        > R = 255
        > G = (100 - percent) / 100 * 255
        > B = 0
        > End Select
        >
        > ColorValue = Color.FromArgb( R, G, B)
        >
        > End Function
        >
        > It works perfectly well, but I wondered if anyone had an alternative
        > approach. For some reason it just doesn't feel satisfying and I have this
        > nagging feeling that there's a more elegant or clever method that I'm not
        > thinking of or that I'm simply not aware of. And for the sake of my own
        > education I'd like to hear any new ideas.
        >
        > Thanks. Sorry for the length of the post.
        >[/color]


        Comment

        Working...