Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasperkamperman/02231f174b867fcb6812 to your computer and use it in GitHub Desktop.
Save kasperkamperman/02231f174b867fcb6812 to your computer and use it in GitHub Desktop.
Change the contrast of input values.
// Check: https://plus.google.com/+KasperKamperman/posts/NmCDNXUVHYs
// #include "FastLED.h"
uint8_t inputValues[7] = {0, 30, 64, 128, 192, 221, 255};
int contrastValue = 8;
void setup() {
Serial.begin(57600);
}
void loop() {
Serial.print("contrast: ");
Serial.println(contrastValue);
// display input
Serial.print("input :");
for(int i = 0; i < 7; i++) {
printWhiteSpace(inputValues[i]); // aligning
Serial.print(inputValues[i]);
Serial.print(", ");
}
Serial.println();
// display contrast
Serial.print("output :");
for(int i = 0; i < 7; i++) {
int outputValue = applyContrast(inputValues[i], contrastValue);
printWhiteSpace(outputValue);
Serial.print(outputValue);
Serial.print(", ");
}
Serial.println();
Serial.println("=============================================");
delay(500);
contrastValue = contrastValue + 10;
if(contrastValue>254) contrastValue = 8;
}
void printWhiteSpace(int val) {
if(val>0)
{ if(val<10) Serial.print(" ");
else if(val<100) Serial.print(" ");
else Serial.print(" ");
}
else
{ if(val==0) Serial.print(" ");
else if(val>-10) Serial.print(" ");
if(val>-100) Serial.print(" ");
}
}
// constrast factor is from 1.0 - 2.0 or 0 - 255
int applyContrast(uint8_t input, uint8_t contrast_factor) {
// around 0
int inputShift = (int) input - 128; // -128 - 128
// 1 + (i * scale) / 256;
inputShift = inputShift + (inputShift * contrast_factor) / 256;
return (int) inputShift + 128;
}
// below two functions 16bit and 8bit (didn't test it with this code, but I use them in other projects)
// constrast factor is from 1.0 - 2.0 or 0 - 255
uint8_t applyContrast8(uint8_t input, uint8_t contrast_factor) {
// around 0
int inputShift = (int) input - 128; // -128 - 128
// 1 + (i * scale) / 256; // +128 shift back
inputShift = 128 + inputShift + (inputShift * contrast_factor) / 256;
return (uint8_t) constrain(inputShift,0,255);
}
// constrast factor is from 1.0 - 2.0 or 0 - 255
uint16_t applyContrast16by8(uint16_t input, uint8_t contrast_factor) {
// scaling around point / dilation middle grey = 32768
// typecast to long otherwise we don't have enough space
int32_t inputTest = input + (int32_t(input - 32768) * contrast_factor) / 256;
return (uint16_t) constrain(inputTest,0,65535);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment