Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - WinAPI ColorChoose Dialog box

Author
Message
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 18th Mar 2011 00:45 Edited at: 18th Mar 2011 11:57
Here is an example in calling an WinAPI colorChoose dialog box.

You will also need IanM's Matrix Plugin set.



I hope this is useful to someone

>Edit<
Fixed minor bug with reversed R & B channels.

>Edit 2<
Changed output to string, not the neatest solution, but it works

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 18th Mar 2011 03:26 Edited at: 18th Mar 2011 11:55
This would be handy to have!

Error 1 - "Constant name 'true' cannot share the name of a reserved word or command."

No prob, I just replaced "true" with "1" in the one placed you used it. You never used the constant "false" so I removed it as well.

Error 2 - It's not giving me the correct color. When I pick pure blue, it gives me pure red (FF0000). When I pick pure red, it gives me pure blue (FF). But when I pick green, I get green (FF00). Something about the position of the rgb values? Maybe a big/little endian prob?

I'll try to code a fix and post.

Also - It should be possible to allow editing the custom colors, but if you write them with random values every time you open the picker, that would preclude saving them. I'll check to see if there is a parameter that lets them be saved/edited.

EDIT:

Using this:


instead of this:


seems to get the byte order in correct position. So here's what I came up with for the whole thing... except that the DWORD returned is negative, so a "cancel" actually returns a valid color, white. So maybe the color needs to be inverted after it comes back from the function, so that the function can return -1 on a cancel?

Hmm...
FURTHER EDIT: I just changed it to return a 1 instead of -1 for a cancel, then it works fine.

MORE EDIT: Aha! Declaring "result" as DWORD does the trick, so the function CAN return -1 as a "cancel".





For the custom colors...
lpCustColors
COLORREF
A pointer to an array of 16 values that contain red, green, blue (RGB) values for the custom color boxes in the dialog box. If the user modifies these colors, the system updates the array with the new RGB values. To preserve new custom colors between calls to the ChooseColor function, you should allocate static memory for the array. To create a COLORREF color value, use the RGB macro.

Well, I've no clue how to allocate static memory. Maybe declaring the array of 16 colors outside of the function, and restoring them when the function is called?

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 18th Mar 2011 10:44 Edited at: 18th Mar 2011 10:52
@Rich:
Yes, I did notice the bug with the Blue and red, but not until AFTER I had posted it here. Fixed now.

I'm guessing the problem u had with the constants is caused by a plugin u have installed that I don't? the true/false declarations are something I instinctively do at the beginning of every project.

As for the custom palette, it's just randomising the values as an example. In the proper application, it's getting the values from an array defined elsewhere in the program.

>Edit<
Hmmm, not fixed apparantly. Works ok but if you select White it still registers as a cancel.

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 18th Mar 2011 10:53 Edited at: 18th Mar 2011 11:05
You could be right about the plugins, I've kind of lost track of which ones I've got installed.

So for the custom palette, I could just keep track of them in a seperate array, stuff them into the memblock when the function is called, and then...? Get them back out of the memblock and stuff them back in the array?

Anyways, thanks for posting this. I for one will get great use out of it!

EDIT: Yah, just noticed the prob with white again. I've just taken to returning a "1" and checking for that instead of "-1", seems to work okay. It seems that 0xFFFFFFFF wraps around to -1. So

INK -1,0

actually uses white. Good to know.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 18th Mar 2011 11:30
@Rich
The problem with using 1 as a return value is 1=rgb(0,0,1)? which is kinda black with a hint of blue.

I have fixed the problem (kinda) by getting the function to return a string containing either "cancel" or hex$(colour).

You can use IanM's hex to decimal() command to change it to a number to use in ink commands and stuff.

I have also noticed that the red/blue bug is apparant when you use the requester several times. e.g Open it, pick "Blue" (0,0,255); close it, open it again and the selected colour is shown as "red" (255,0,0).

This serves me right for posting code snippets at 1am!

I'll get it fixed and post a final version of this code here shortly.

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 18th Mar 2011 11:52
=PRoF=

Because the alpha channel is added in, the color rgb(0,0,1) is returned as 4278190081 instead of 1, so checking for "1" as a cancel works fine in the example I posted, although I haven't tried it with the SET ALPHA command.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 18th Mar 2011 12:09
Ok guys, final version of the code posted now.

@Rich:
I'm not sure tbh, but it's a possibility so I changed it over to outputting a string rather than a numerical value.

Thanks for your help testing this

Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 18th Mar 2011 12:29
You're welcome, and thanks again for the code!

5:30 am here, so I'm off to bed for a couple of hours.

Login to post a reply

Server time is: 2024-11-21 11:50:18
Your offset time is: 2024-11-21 11:50:18