trackBar亮度控制不会将整个颜色范围从黑色滚动到白色

时间:2015-03-30 04:34:54

标签: c# winforms brightness trackbar

我有一个轨迹栏控件,用于调整窗体背景颜色的亮度。最后,我会修改它以调整图像的亮度。如果您查看亮度滑块在Microsoft colorDialog Control中的工作原理,当您上下移动时,值会从白色变为黑色,但在中间,原始颜色的色调永远不会丢失。最初,我失去了色调,只是做了灰度调整,但我终于找到了解决方法。

我目前的问题是我无法调整整个颜色范围。例如,当选择颜色R 192,G 128,B 255时,我可以一直向下滚动到黑色,并一直回到我的原始颜色,但我无法向上滚动到白色。我现在每天花几个小时研究这个代码,持续10天,要么从黑到白,失去色调(已经解决了),要么得到当前的问题:我可以从lavendar滚动到黑色到lavendar ,但不能滚动到白色。我非常感谢一些帮助。谢谢。

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); 
}

这是我的第一篇文章,我阅读了关于不提出重复问题的新帐户声明。我已经研究了这个,并且在此之前没有看过这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

在代码中,变量xRed, xGreen and xBlue将根据表单背景颜色的rgb值分配一个值。例如,如果表单的背景颜色的RGB值为(150,170和180),那么xRed将为0.588,G将为0.667,B将为0.705

当您将曲目栏移动到最大值(比如255)时,表单的背景颜色为bgRed = xRed * trackBar1.value(150),bgGreen为170,bgBlue为180,这是原始背景颜色。形式。

在我看来,以100%亮度显示原始色彩是正确的行为。如果要将白色显示为100%的背景颜色,则应将代码更改为如下所示

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); 
}