stevesketch
1/26/2016 - 2:13 AM

Colour Manipulations

Colour Manipulations

int getBrightness (int c)
{ 
  int r=c>>16&0xFF;
  int g=c>>8&0xFF;
  int b=c&0xFF; 
  return c=(c=r>g?r:g)<b?b:c;
} 
color slightlyRandomColor(color inputCol, float rand) {
  colorMode(HSB, 255);
  float h = hue(inputCol);
  h = (h+random(-rand, rand))%255;
  color col = color(h, saturation(inputCol), brightness(inputCol));
  colorMode(RGB);
  return col;
}
color lerpPalette(color[] pal, float t) {

  float scaledT = t* (pal.length-1);
  color prevC = pal[(int)scaledT];
  color nextC = pal[(int)(scaledT+1f)];
  float newT = scaledT - (float)((int)scaledT); 

  return lerpColor(prevC, nextC, newT);
}


color lerpPaletteList(ArrayList<Integer> pal, float t) {

  float scaledT = t* (pal.size()-1);
  color prevC = pal.get((int)scaledT);
  color nextC = pal.get((int)(scaledT+1f));
  float newT = scaledT - (float)((int)scaledT); 

  return lerpColor(prevC, nextC, newT);
}
color exposeRGB(color cc, int amount)
{
  int red = (int) (red(cc) + amount);
  int green = (int) (green(cc)+ amount);
  int blue = (int) (blue(cc)+ amount);
  color ccnew = color(red, green, blue);
  return ccnew;
}
color getAverageColor(PImage img) {
  colorMode(RGB,255);
  img.loadPixels();
  int r = 0, g = 0, b = 0;
  for (int i=0; i<img.pixels.length; i++) {
    color c = img.pixels[i];
    r += c>>16&0xFF;
    g += c>>8&0xFF;
    b += c&0xFF;
  }
  r /= img.pixels.length;
  g /= img.pixels.length;
  b /= img.pixels.length;
  return color(r, g, b);
}
color noisedColor(color inputCol, float rand, float freq) {
  colorMode(HSB, 255);
  float h = hue(inputCol);
  h = h+((noise(freq)-0.5)*rand);
  color col = color(h, saturation(inputCol), brightness(inputCol));
  colorMode(RGB);
  return col;
}
// forum.processing.org/two/discussion/3270/
// how-to-get-pixel-intensity-not-brightness
 
noLoop();
background((color) random(#000000));
 
loadPixels();
final color c = pixels[(int) random(pixels.length)];
 
final color luminG = c>>010 & 0xFF;
final float luminRangeG = luminG/255.0;
 
println("#" + hex(c, 6));
println(luminG);
println(luminRangeG);
factor = (259 * (contrast + 255)) / (255 * (259 - contrast))
colour = GetPixelColour(x, y)
newRed   = Truncate(factor * (Red(colour)   - 128) + 128)
newGreen = Truncate(factor * (Green(colour) - 128) + 128)
newBlue  = Truncate(factor * (Blue(colour)  - 128) + 128)
PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
color complementColor(color inputCol) {
  colorMode(RGB);
  float R = red(inputCol);
  float G = green(inputCol);
  float B = blue(inputCol);
  float minRGB = min(R, min(G, B));
  float maxRGB = max(R, max(G, B));
  float minPlusMax = minRGB + maxRGB;
  color complement = color(minPlusMax-R, minPlusMax-G, minPlusMax-B);
  return complement;
}
float colorDist(color c1, color c2)
{
  float rmean =(red(c1) + red(c2)) / 2;
  float r = red(c1) - red(c2);
  float g = green(c1) - green(c2);
  float b = blue(c1) - blue(c2);
  return sqrt((int(((512+rmean)*r*r))>>8)+(4*g*g)+(int(((767-rmean)*b*b))>>8));
} 
CMYK -> CMY 
CMYK values = From 0 to 1 
C = ( C * ( 1 - K ) + K ) 
M = ( M * ( 1 - K ) + K ) 
Y = ( Y * ( 1 - K ) + K ) 
 
-- 
 
CMY -> RGB 
CMY values = From 0 to 1 
 
R = ( 1 - C ) * 255 
G = ( 1 - M ) * 255 
B = ( 1 - Y ) * 255 
 
-- 
 
RGB -> CMY 
RGB values = From 0 to 255 
  
C = 1 - ( R / 255 ) 
M = 1 - ( G / 255 ) 
Y = 1 - ( B / 255 ) 
  
-- 
 
CMY -> CMYK 
CMY values = From 0 to 1 
  
var_K = 1 
  
if ( C < var_K )   var_K = C 
if ( M < var_K )   var_K = M 
if ( Y < var_K )   var_K = Y 
  
C = ( C - var_K ) / ( 1 - var_K ) 
M = ( M - var_K ) / ( 1 - var_K ) 
Y = ( Y - var_K ) / ( 1 - var_K ) 
K = var_K