Reputation: 662
private readonly ConcurrentDictionary<string, System.Drawing.Color> _colorSet;
public void BuildColorSet(IList<string> colorNames, string prefix, bool forceLastToGray)
{
var size = forceLastToGray ? colorNames.Count - 1 : colorNames.Count;
int nbHue = 6;
int nbCycle = (int)Math.Ceiling((double)size / nbHue);
var saturationMax = nbCycle <= 2 ? 1.0 : 1.0;
var saturationMin = 0.3;
var luminanceMax = nbCycle <= 2 ? 0.85 : 0.85;
var luminanceMin = 0.3;
var maxSaturationShift = 0.30;
var maxLuminanceShift = 0.15;
var interval = 1.0 / Math.Min(size, nbHue);
var saturationShift = (saturationMax - saturationMin) / (nbCycle - 1);
saturationShift = Math.Min(saturationShift, maxSaturationShift);
var luminanceShift = (luminanceMax - luminanceMin) / (nbCycle - 1);
luminanceShift = Math.Min(luminanceShift, maxLuminanceShift);
var hueShift = 0.0;
var saturation = saturationMax;
var luminance = luminanceMax;
for(var i = 0; i<size; i++)
{
if(i > 0 && (i % nbHue == 0)) // Next Cycle
{
saturation -= saturationShift;
luminance -= luminanceShift;
hueShift = hueShift == 0 ? interval/2 : 0;
}
var hue = interval*(i%nbHue) + hueShift;
System.Drawing.Color color = HSL2RGB(hue, saturation, luminance);
_colorSet.AddOrUpdate(prefix + colorNames[i], color, ???);
}
if(forceLastToGray)
{
_colorSet.TryAdd(prefix + colorNames[colorNames.Count - 1], System.Drawing.Color.LightGray);
}
_cssDirty = true;
}
I want to be able to update the dictionary if the color exists with new value. And also add to dictionary if the color is not there in dictionary.
I am using the AddOrUpdate but not able to get the 3rd parameter(form the lambda expression OR delegate method) of the AddOrUpdate method.
Any idea how my 3rd parameter would look like?
Upvotes: 9
Views: 16871
Reputation: 41867
It looks like you don't care if the color is already there; you always want to update the value in the dictionary. In that case you're better off using the normal indexer, e.g.
_colorSet[prefix + colorNames[i]] = color;
Upvotes: 0
Reputation: 3012
From the documentation:
updateValueFactory Type: System.Func The function used to generate a new value for an existing key based on the key's existing value
This will leave the value in the collection alone if it already exists:
_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
return existingVal;
});
This will replace the value in the collection with the same one specified for the insert:
_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
return color;
});
You can perform conditional logic, comparisons between the old value and new value, or update the original object in the function, for example.
_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
if (existingVal.Name == "Red")
return existingVal;
else
return color;
});
Upvotes: 25
Reputation: 13743
As per the web page asawyer gave you, what's required is a function
Func<TKey, TValue, TValue>
In this case it looks like you are passing a string and a Color but how you want to combing them is largely upto you. You need a function that returns a Color so the following should work from a syntax perspective.
(key, oldValue) => oldValue
I've no idea who you might calculating the new value. You could for example use your new color
_colorSet.AddOrUpdate(prefix + colorNames[i], color, (key, oldValue) => color);
Upvotes: 1