【发布时间】:2015-03-30 04:34:53
【问题描述】:
我有一个用于调整表单背景颜色亮度的轨迹栏控件。最后,我将对其进行修改以调整图像的亮度。如果您查看 Microsoft colorDialog 控件中的亮度滑块是如何工作的,当您上下移动时,值会从白色变为黑色,但在中间,原始颜色的色调永远不会丢失。最初,我失去了色调,只是进行了灰度调整,但我终于找到了解决方法。
我目前的问题是我无法调整整个颜色范围。 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.我已经每天处理这个代码几个小时了 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);
}
这是我的第一篇文章,我阅读了关于不问重复问题的新帐户声明。我对此进行了研究,并且在这里之前没有看到过这个问题。谢谢你。
【问题讨论】:
-
使:(双)(this.BackColor.R / 256f);
-
该更改对我的代码运行方式没有任何影响。你有什么想法?谢谢
-
你需要避免整数除法的陷阱! (您是否包括了 f(浮点数)?(可能还有其他需要注意的地方..)任何
-
@MaoTseTongue,你的要求是从
到 到 到 ? -
嗯,不是立即改变,而是从任何颜色逐渐滚动到黑色,再到任何颜色到白色,就像 MS 颜色对话框中的亮度控制一样。
标签: c# winforms brightness trackbar