want to get color value from a Gradient color bar
generate a rectangle with
LinearGradientBrush(rect,Color.Red,Color.Gray,LinearGradientMode.Horizontal).
i want to get any point color value in the rectangle , how can i do?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
tang_ericPosted Jul 30, 2004, 3:20 AM
MykoninePosted Jul 26, 2004, 4:25 AM
/*I use the MouseUp event only because it's the last color the user chooses. You could also use the MouseMove event so that the color stays updated.*/ public void Form_MouseUp(object sender, System.MouseEventArgs e) { //make sure the mouse click is in the rectangle if(e.Y > this.gradientRectangle.Y + this.gradientRectangle.Height || e.Y < this.gradientRectangle.Y) return; Color selectedColor; //get how far away the mouse click is from the left side of the rectangle to the right side. double amountChange = (double)(e.X - rectLeft)/(rectRight - rectLeft); //if the mouse click is in the rectangle if(amountChange >= 0 && amountChange <= 1) { //get the new RGB values based on the distance from the left of the rectangle to the right. int nR, nG, nB; nR = (int)(rightColor.R * amountChange + leftColor.R * (1 - amountChange)); nG = (int)(rightColor.G * amountChange + leftColor.G * (1 - amountChange)); nB = (int)(rightColor.B * amountChange + leftColor.B * (1 - amountChange)); selectedColor = Color.fromArgB(nR,nG,nB); } }