wpf - C#.Net Unspecified String to Color Converter -


in situation have list of customers represent colors. how make converter generate color based off customer name, or portion of name?

public class customer {     public string customername {get; set;}      //other properties unrelated question.... } 

then in xaml somewhere able following:

<textblock text="{binding customername}"            foreground="{binding customername,             converter={staticresource mystringtocolorconverter}}"/> 

i don't need whole converter written out, ideas on how convert unspecified string color.

you take binary values of first 3 characters of name , convert red, green, , blue values. thing though regular names begin (assuming upper case characters) 26 of 256 possible binary values you'll need adding , multiplying in 0-1 range. if want more 26 discrete values each of red, green, , blue consider aggregating value multiple letters.

once have number in 0-1 range, next thing want normalize values avoid super dark or super bright colours. possible way use standard normalization methods follows.

intensity = sqrt(r*r + g*g + b*b) if(intensity != 0) {     r = r / intensity;     g = g / intensity;     b = b / intensity; } 

this used quite in computer graphics, create vector of length 1 in same direction original vector. in case we're treating rgb value vector. think of this, imagine rgb colour space 3d plane, red x axis, green y axis, , blue z axis; equation gives colour on sphere radius 1 (including pure red, pure green, pure blue, , combinations in between.)

the last step multiplying each of rgb 255 in range of [0,255).

this 1 of problems should mess around , experiment yourself, it's quite interesting.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -