Color box

  • Thread starter Thread starter Zach
  • Start date Start date
Z

Zach

When someone selects a custom colour, that colour is placed in one of
the boxes below the regular colour. How can that custom colour be
retained after closing down the colour box and re-opening it?

What I am asking is (I think): how to do this process in reverse

//dlg.CustomColors = new int[]{6916092, 15195440, 16107657,
//1836924,3758726, 12566463, 7526079, 7405793, 6945974, //241502,
2296476, 5130294,3102017, 7324121, 14993507, //11730944,};

If you don't know, do you know how to convert a colour into numbers like
those above?

Please, can someone shed light on this problem?
Zach
 
By default I would have said ToARGB(), but according to MSDN these
numbers are atually BGR:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.colordialog.customcolors.aspx

The following puts a color both in and out - that do?

Color color = Color.Tomato;
int bgr = (color.B << 16) | (color.G << 8) | color.R;
using(ColorDialog dlg = new ColorDialog()) {
dlg.CustomColors = new int[] { bgr };
dlg.ShowDialog();
foreach (int outBgr in dlg.CustomColors) {
// store these in some form
int r = outBgr & 0xFF,
g = (outBgr & 0xFF00) >> 8,
b = (outBgr & 0xFF0000) >> 16;
Trace.WriteLine(string.Format("R:{0}, G:{1}, B:{2}",
r, g, b));
}
}

Marc
 
Marc Gravell said:
By default I would have said ToARGB(), but according to MSDN these numbers
are atually BGR:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.colordialog.customcolors.aspx

The following puts a color both in and out - that do?

Color color = Color.Tomato;
int bgr = (color.B << 16) | (color.G << 8) | color.R;
using(ColorDialog dlg = new ColorDialog()) {
dlg.CustomColors = new int[] { bgr };
dlg.ShowDialog();
foreach (int outBgr in dlg.CustomColors) {
// store these in some form
int r = outBgr & 0xFF,
g = (outBgr & 0xFF00) >> 8,
b = (outBgr & 0xFF0000) >> 16;
Trace.WriteLine(string.Format("R:{0}, G:{1}, B:{2}", r, g,
b));
}
}

Marc

Sorry, I am not familiar with this kind of coding:
int bgr = (color.B << 16) | (color.G << 8) | color.R;
I couldn't get it to work. Presumably I needed System Diagnistics?
The reference you gave is from where I quoted the example in the query I
posted.
Thank you for the trouble you took in responding, apologies about my
limitations.
If you have time please give me a little more context so I can try out your
code.
Many thanks,
Zach.
 
You only need System.Diagnostics for the Trace.WriteLine - not the
"real" code.

<< is the "left shift" operator
Colours are internally held as a mangled form of an integer, and you
often need to use binary arithmetic to work with them. The following
is just a very quick overview...

"int" is (to the computer) just a 32-bit buffer. Each of the
red/blue/green parts is an 8-bit value (giving 256 tones in each
color-axis - i.e. 24-bit colour).
The acronym BGR means store these 3 sets of 8 bits in the sequence:
xxxxxxxxbbbbbbbbggggggggrrrrrrrr
the first 8 bits are unimportant; the next 8 bits represent the "blue"
(from 0 to 255), the next 8 bits the "green" (from 0 to 255), etc.
Currently, our 3 composite colours (in the range 0-255) are each
occupying the rightmost 8 bits in their individual ints; to combine
them we "shift" the blue value left by 16 bits, we shift the green
value left by 8 bits; we then use "bitwise or" (the pype symbol: |) to
comine the set-bits from the three values. I'm glossing over something
called endian-ness, but that shouldn't really concern us here.

At the bottom, I reverse the process by reading "r" from the last 8
bits, g from the next 8 bits to the left (remembering to "shift" them
right by 8 bits back into the 0-255 range, using >>), and finally b
(the next 8 bits to the left, shifted 16 bits).

For reference, 0xFF00 is just shorthand for the binary sequence
"00000000000000001111111100000000", and is used to create a mask to
indicate which bits I am interested in (binary reads right to left, so
0xFF as an int is 24 0s followed by 8 1s)

Once you have the red, green and blue you can store in a simple to
read form somewhere, ready to go through the entire process again.

For comparison, more frequently colours are stored as "ARGB" which
uses 4 blocks of 8 bytes for the "alpha", "red", "green" and "blue"
(left-to-right).

<phew!>

Marc
 
Hi Marc,

Wouw, man, that is interesting. That isn't the sort of programming I am
familiar with. Thank you very much for your explanation! I will have to
spend some time on what you wrote to absorb it.

Regards,
Zach.

This doesn't do a thing by the way:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsApplication2
{
public partial class Form1
{
Button my_button;
public Form1()
{

Color color = Color.Tomato;
int bgr = (color.B << 16) | (color.G << 8) | color.R;
using (ColorDialog dlg = new ColorDialog())
{
dlg.CustomColors = new int[] { bgr };
dlg.ShowDialog();
foreach (int outBgr in dlg.CustomColors)
{
// store these in some form
int r = outBgr & 0xFF, g = (outBgr & 0xFF00) >> 8, b = (outBgr & 0xFF0000)Trace.WriteLine(string.Format("R:{0}, G:{1}, B:{2}", r, g, b));
}
}
}
}
}
 
Back
Top