1

I have a trackbar control that is adjusting the brightness of the background color of a form. Eventually, I'll modify it to adjust an image's brightness. If you look at how the brightness slider works in the Microsoft colorDialog Control, as you move up and down, the value changes from white to black, but in the middle, the hue of the original color is never lost. Originally, I was losing the hue, and just doing a grayscale adjustment, but I finally found the work around for that.

My current problem is that I cannot adjust the entire color range. For example, when selecting the color R 192, G 128, B 255, I can scroll all the way down to black, and all the way back up to my original color, but I can't scroll up to white. I've been working on this code a few hours a day for 10 days now, and either get from black to white and lose the hue (that is resolved), or get the current issue: I can scroll from lavendar to black to lavendar, but can't scroll up to white. I'd deeply appreciate some assistance. Thank you.

public partial class Form1 : Form
{

   double xRed, xGreen, xBlue, xColor; 
   bool count = true;

    private void Form1_BackColorChanged(object sender, EventArgs e)
    {
        //Do this once, when user sets a new BG color 
        if (count == true)
        {                     
            //Get a % of the color value instead of the absolute value,  when using absolute value, if it goes down near 0, or up near 255, hue is lost.  
            xRed = (double) this.BackColor.R / 255; 
            xGreen = (double) this.BackColor.G / 255; 
            xBlue = (double) this.BackColor.B / 255;

            xColor = (((xRed + xGreen + xBlue) * 255 ) / 3); 
            xColor = Math.Round(xColor, 0);

            //Label is just so I can see RGB and trackBar values 
            lblBGColor.Enabled = true; 
            trackBar1.Enabled = true; 
            trackBar1.Value = (int)xColor; 
            label1.Text = Convert.ToString(trackBar1.Value);
            count = false; 
        }      
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        double bgRed = 0, bgGreen = 0, bgBlue = 0;
        //Once it goes down to black or up to white, restore the hue value as a percent. Percent of R (value / 255), Percent of G (value / 255), Percent of B (value / 255). SO if red is 196 and 196 is 80% of 255, red is 80% of trackbar value, if green is 128 and 128 is 50% of 255, green is 50% of trackbar value, etc... 

        //I suspect the problem is here, with trackBar1.Value.  I also tried adding the trackBar1.Value change since last scroll, but that didn't help.  Also tried setting trackBar1.Value to 128, but no help.  
        bgRed = xRed * trackBar1.Value;
        bgGreen = xGreen * trackBar1.Value; 
        bgBlue = xBlue * trackBar1.Value; 

        bgRed = Math.Round(bgRed, 0);
        bgGreen = Math.Round(bgGreen, 0);
        bgBlue = Math.Round(bgBlue, 0); 

        if (bgRed > 255)
            bgRed = 255;
        if (bgGreen > 255)
            bgGreen = 255;
        if (bgBlue > 255)
            bgBlue = 255;

        //Avoid unneeded looping through BackColorChanged event   
        count = false;  
        //Set form and label values 
        this.BackColor = Color.FromArgb(255, (int)bgRed, (int)bgGreen, (int)bgBlue); 
        label1.Text = Convert.ToString((int)bgRed + " " + (int)bgGreen + " " + (int)bgBlue + " trackBar: " + trackBar1.Value); 
}

This is my first post, I read the new account statement about not asking duplicate questions. I have researched this, and have not seen this question asked before here. Thank you.

MaoTseTongue
  • 71
  • 2
  • 8
  • make that: (double)( this.BackColor.R / 256f); – TaW Mar 30 '15 at 04:57
  • That change did not make any difference in the way my code ran. What did you have in mind? Thank you – MaoTseTongue Mar 30 '15 at 06:36
  • You need to avoid the pitfalls of integer division! (Did you include the f (float)? (There may be other spots to watch for this..) Any byte value < 255 div by 255 is 0, and casting this 0 to double is too late! – TaW Mar 30 '15 at 07:08
  • @MaoTseTongue, your requirement is to change from to to to ? – Kira Mar 30 '15 at 09:40
  • Well, not to change instantly, but to scroll gradually from any color to black to any color to white, like the brightness control in the MS Color Dialog Box does. – MaoTseTongue Mar 31 '15 at 01:29

1 Answers1

0

In your code the variables xRed, xGreen and xBlue will be assigned a value depending upon the rgb value of the form's background color. For example if the form's background color has RGB value of (150, 170 and 180) then xRed will be 0.588, G will be 0.667 and B will be 0.705

When you move your track bar to maximum (say 255), then background color of your form will be bgRed = xRed * trackBar1.value (150), bgGreen will be 170 and bgBlue will be 180 which is the original background color of the form.

In my opinion, displaying original color at 100% brightness is the correct behavior. If you want to display white as background color at 100% you should change your code to something like below

private void trackBar1_Scroll(object sender, EventArgs e)
    {
        double bgRed = 0, bgGreen = 0, bgBlue = 0;
        if(this.trackBar1.value==this.trackBar1.Maximum)
        {
            bgRed=255;
            bgGreen=255;
            bgBlue=255;
        }

       else{
            //Once it goes down to black or up to white, restore the hue value as a percent. Percent of R (value / 255), Percent of G (value / 255), Percent of B (value / 255). SO if red is 196 and 196 is 80% of 255, red is 80% of trackbar value, if green is 128 and 128 is 50% of 255, green is 50% of trackbar value, etc... 
        //I suspect the problem is here, with trackBar1.Value.  I also tried adding the trackBar1.Value change since last scroll, but that didn't help.  Also tried setting trackBar1.Value to 128, but no help.  
        bgRed = xRed * trackBar1.Value;
        bgGreen = xGreen * trackBar1.Value; 
        bgBlue = xBlue * trackBar1.Value; 

        bgRed = Math.Round(bgRed, 0);
        bgGreen = Math.Round(bgGreen, 0);
        bgBlue = Math.Round(bgBlue, 0); 

        if (bgRed > 255)
            bgRed = 255;
        if (bgGreen > 255)
            bgGreen = 255;
        if (bgBlue > 255)
            bgBlue = 255;
    }
    //Avoid unneeded looping through BackColorChanged event   
    count = false;  
    //Set form and label values 
    this.BackColor = Color.FromArgb(255, (int)bgRed, (int)bgGreen, (int)bgBlue); 
    label1.Text = Convert.ToString((int)bgRed + " " + (int)bgGreen + " " + (int)bgBlue + " trackBar: " + trackBar1.Value); 
}
Kira
  • 1,403
  • 1
  • 17
  • 46
  • I appreciate your thoughts, but that's not really a complete solution. The color scrolls gradually from lavendar down to black and gradually back up to lavendar, and then when it moves from trackBar1.Value 254 to trackBar1.Value 255, it makes a sudden, and certainly not gradual or subtle, jump to white. It moves gradually to black, I want it to also move gradually to white. Thanks – MaoTseTongue Mar 30 '15 at 06:45
  • My suggestion would be change your approach. Your current implementation makes original background color as current background color of the form when maximum value is reached in track bar. I'll modify my code soon – Kira Mar 30 '15 at 08:52