Doing nice stuff with Colors - the modern way !?

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Doing nice stuff with Colors - the modern way !?

Post by Phil Hepburn »

Hi guys,

I thought some of you may like to share in the fun I have been having playing around with colors in the 'QR code' applet I have been developing for the German Cologne conference.

Although it is only a small app - not many code and script lines, in fact it uses some quite neat and modern ways of doing things. I even surprised myself.

Have a look at this included short video to see how it now works, and then we can discuss further.

[The extension mp4 has been deactivated and can no longer be displayed.]

The parts of the app I have added since I last posted, are those to support the two new drop lists (combo boxes) with 150 colors available for selection.

The code uses Reflection to find all of the named color 'properties' in the System.Drawing namespace. these are then converted by code to Names and Colors.

The two drop lists in fact use the same 'script and code behind' - so once you have cracked one you have done the other too - nice.

The issues I tried to solve were around the strength (or darkness) of the colours, and getting a text name and color to show on each different background colour. There was need for a little algorithm - Alwyn taught me to approach things this way (shown in the algorithm) ;-0) Thanks Alwyn.

Oh! I also nearly forgot to say that I also use LINQ to grade the colors - dark to light in the lists. Lets look at the part of the drop list where it changes from dark to lighter, and the text shown swaps from white to black - check below :-
ColorsWith QRcodes_03.jpg
ColorsWith QRcodes_03.jpg (62.07 KiB) Viewed 196 times
Okay then, getting there, just a bit further ............

I have added a 'Converter' to the XAML script which styles the 'ComboBoxItem' GUI elements.

Here it is below, and basically it takes the Background property value and returns either a Black or a White as Foreground color, depending on the 'darkness' of the background color. Check this out :-
ColorsWith QRcodes_01.jpg
ColorsWith QRcodes_01.jpg (206.66 KiB) Viewed 196 times
Sorry - when I took this screen shot I had not as then sorted the colors into order - Dark to Light.

All the interactivity is done in WPF and by 'data binding' in the XAML script.

And finally, the nice part played by LINQ - the few lines in the next image place the color related items into order - check lines 128 through 135 :-
ColorsWith QRcodes_02.jpg
ColorsWith QRcodes_02.jpg (186.48 KiB) Viewed 196 times
The selected items from the two drop lists are held as public properties on the Window class used. These behave as they should, as far as notifying property changes.

It is quite amazing just how much of this simple sample is modern style code/script, and would not really have been recognisable five years ago. And I know I for one, would NOT have been able to tackle this test 'task'sample back then.

The question "is the result worth all the hard work to keep up with changes" needs to be asked and fairly answered I feel.

Oh! If you would like to download the working EXE file then here is the link :-

https://1drv.ms/f/s!AiCBl-gBWjY9hepsr0_B12mrh2vQsg

Good Luck,
Phil.
Wales, UK.

P.S. the attendees at my sessions, and the Cologne conference will get all of the supporting material - code, script, projects and solutions, as well as eNotes. It is great fun! We will have a good time I feel !!
Terry
Posts: 306
Joined: Wed Jan 03, 2018 11:58 am

Doing nice stuff with Colors - the modern way !?

Post by Terry »

A nifty little program Phil.

Useful in many, many ways.

Terry
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Doing nice stuff with Colors - the modern way !?

Post by Phil Hepburn »

Hi Terry,

You have probably forgotten - BUT - it was you who first drew my attention to that fact that we could bind across "properties / attributes" in the same Control element. You helped me get my first similar sample going - however, I hand coded the color items back then. And no nifty color algorithms back then.

So thanks for the 'leg up' as they say.

Have a nice day - it lovely and bright and sunny in Newport - if a bit on the 'chilly' side!
Cheers,
Phil.
Post Reply