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 great article http://devmag.org.za/2012/07/29/how-to-choose-colours-procedurally-algorithms/ had this implementation.
I have pending to port the algo to javascript and use it on potaje.
The article is down now due to the account being suspended, which is sad because it was a great article.

@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