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.