Skip to content

Instantly share code, notes, and snippets.

@netskink
Created February 7, 2016 16:45
Show Gist options
  • Save netskink/d3637d334ee98d682a8b to your computer and use it in GitHub Desktop.
Save netskink/d3637d334ee98d682a8b to your computer and use it in GitHub Desktop.
func adjRed(inout pixel:Pixel, by:Int) {
let redDiff = Int(pixel.red) - avgRed
if (redDiff > 0) {
pixel.red = UInt8(max(0,min(255, avgRed + redDiff * by)))
}
}
func adjGreen(inout pixel:Pixel, by:Int) {
let greenDiff = Int(pixel.green) - avgGreen
if (greenDiff > 0) {
pixel.green = UInt8(max(0,min(255, avgGreen + greenDiff * by)))
}
}
func adjBlue(inout pixel:Pixel, by:Int) {
let blueDiff = Int(pixel.blue) - avgBlue
if (blueDiff > 0) {
pixel.blue = UInt8(max(0,min(255, avgBlue + blueDiff * by)))
}
}
func adj(inout pixel:Pixel, color:String, by:Int) {
switch color {
case "Red":
adjRed(&pixel, by:by);
case "Green":
adjGreen(&pixel, by:by);
case "Blue":
adjBlue(&pixel, by:by);
default:
print("Invalid color")
}
}
func adj(colorIn:String, by:Int) {
// Load iterate pixels of the image.
for y in 0..<height {
for x in 0..<width {
let index = y * width + x
var pixel = myRGBA.pixels[index]
adj(&pixel, color:colorIn, by:by);
myRGBA.pixels[index] = pixel
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment