Skip to content

Instantly share code, notes, and snippets.

@tutuca
Created February 27, 2015 14:38
Show Gist options
  • Save tutuca/5608efc5e8b620c3db47 to your computer and use it in GitHub Desktop.
Save tutuca/5608efc5e8b620c3db47 to your computer and use it in GitHub Desktop.
Color harmony algorithm
public static List GenerateColors_Harmony(
int colorCount,
float offsetAngle1,
float offsetAngle2,
float rangeAngle0,
float rangeAngle1,
float rangeAngle2,
float saturation, float luminance)
{
List colors = new List();
float referenceAngle = random.NextFloat() * 360;
for (int i = 0; i < colorCount; i++)
{
float randomAngle =
random.NextFloat() * (rangeAngle0 + rangeAngle1 + rangeAngle2);
if (randomAngle > rangeAngle0)
{
if (randomAngle < rangeAngle0 + rangeAngle1)
{
randomAngle += offsetAngle1;
}
else
{
randomAngle += offsetAngle2;
}
}
HSL hslColor = new HSL(
((referenceAngle + randomAngle) / 360.0f) % 1.0f,
saturation,
luminance);
colors.Add(hslColor.Color);
}
return colors;
}
@tutuca
Copy link
Author

tutuca commented Feb 27, 2015

This one takes three colors and create a pallete out of them.

public static Color RandomMix(Color color1, Color color2, Color color3,
float greyControl)
{
int randomIndex = random.NextByte() % 3;

  float mixRatio1 =
     (randomIndex == 0) ? random.NextFloat() * greyControl : random.NextFloat();

  float mixRatio2 = 
     (randomIndex == 1) ? random.NextFloat() * greyControl : random.NextFloat();

  float mixRatio3 = 
     (randomIndex == 2) ? random.NextFloat() * greyControl : random.NextFloat();

  float sum = mixRatio1 + mixRatio2 + mixRatio3;

  mixRatio1 /= sum;
  mixRatio2 /= sum;
  mixRatio3 /= sum;

  return Color.FromArgb(
     255,
     (byte)(mixRatio1 * color1.R + mixRatio2 * color2.R + mixRatio3 * color3.R),
     (byte)(mixRatio1 * color1.G + mixRatio2 * color2.G + mixRatio3 * color3.G),
     (byte)(mixRatio1 * color1.B + mixRatio2 * color2.B + mixRatio3 * color3.B));

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment