473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Color box

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
Nov 16 '07 #1
4 3050
By default I would have said ToARGB(), but according to MSDN these
numbers are atually BGR:
http://msdn2.microsoft.com/en-us/lib...tomcolors.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
Nov 16 '07 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:e9**************@TK2MSFTNGP05.phx.gbl...
By default I would have said ToARGB(), but according to MSDN these numbers
are atually BGR:
http://msdn2.microsoft.com/en-us/lib...tomcolors.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.

Nov 16 '07 #3
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
Nov 16 '07 #4
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)
>16;
Trace.WriteLine(string.Format("R:{0}, G:{1}, B:{2}", r, g, b));
}
}
}
}
}
>
Nov 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
(PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves as a palette-based (8-bit, 256-color) single-color transparancy?? Saving this way, I could be sure that an image loaded from...
6
by: me | last post by:
good day, i found this message: and i want to know more about it too. ========================================== Hey all, example:...
6
by: rzed | last post by:
I'm using PIL to generate some images which may be rotated at the user's option. When they are rotated, the original image is cropped in the new image (which is fine), and the corners are black...
3
by: Richard A. DeVenezia | last post by:
I hope this is the end of my present 'discovery' phase. I've learned alot about JavaScript in a short time and my head hurts. The following is what came out of all my questions and all the...
5
by: Chris Beall | last post by:
I'm displaying an image that is also a link against a black background. In Netscape 7.1, the current background color is displayed as a horizontal bar below the image. This allows :hover effects...
4
by: bart plessers | last post by:
Hello, I am making a website where the user can choose a 'skin'. This works with asp en stylesheets. In the stylesheet, a number of tags are (re)defined. The main idea is to have a limited...
18
by: Jan Tuxen | last post by:
Jakob Nielsen in his most recent Alertbox (http://www.useit.com/alertbox/20040503.html) tells web authors to change the color of visited links. I agree to his purpose: Help users understand...
25
by: Neal | last post by:
According to the CSS lint at http://htmlhelp.org/tools/csscheck/, "The shorthand background property is more widely supported than background-color." Can anyone point me to, or provide, information...
27
by: Kevin Yu | last post by:
When I declare on HTML page <LINK href="mycss.css" type="text/css" rel=stylesheet /> .... <BODY class=myclass> in mycss.css BODY { FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY:...
7
by: Jonathan N. Little | last post by:
When doing a final check on my stylesheets the CSS Validator flag lines like this one as an error: ADDRESS A { color: gold; } Error is: # Line: 31 Context : ADDRESS A Invalid number :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.