Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Are you an F# programer with a liberal point of view?
// Do you want to show your happiness at the advancement of gay right?
// But hate the convience of using a facebook app to rainbowify your profile photo?
// Simple, use this script instead!
open System.Drawing
open System.Runtime.InteropServices
open System.Drawing.Imaging
open System.IO
let rainbowColours =
[| Color.Red // Richard
Color.Orange // of
Color.Yellow // York
Color.Green // Gives
Color.Blue // Battle
Color.Indigo // in
Color.Violet |] // Vain
let rainbowifyBitmap (file: string) =
use bitmap = new Bitmap(file)
let stripeHight = double bitmap.Height / double rainbowColours.Length
let rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height)
let sourceData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
let pixelBuffer: byte[] = Array.zeroCreate (sourceData.Stride * sourceData.Height)
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
let pixelsInStripe = pixelBuffer.Length / rainbowColours.Length
let tintFactor = 350.
for k in 0 .. 4 .. pixelBuffer.Length - 1 do
let tintColour = rainbowColours.[k / pixelsInStripe]
let b = double pixelBuffer.[k]
let g = double pixelBuffer.[k + 1]
let r = double pixelBuffer.[k + 2]
let blue = b + (255. - b) * (double tintColour.B / tintFactor)
let green = g + (255. - g) * (double tintColour.G / tintFactor)
let red = r + (255. - r) * (double tintColour.R / tintFactor)
pixelBuffer.[k] <- byte (min 255. blue)
pixelBuffer.[k + 1] <- byte (min 255. green)
pixelBuffer.[k + 2] <- byte (min 255. red)
use resultBitmap = new Bitmap(bitmap.Width, bitmap.Height);
let rect = new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height)
let resultData = resultBitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length)
resultBitmap.UnlockBits(resultData)
let dir = Path.GetDirectoryName(file)
let name = Path.GetFileNameWithoutExtension(file)
let ext = Path.GetExtension(file)
resultBitmap.Save(Path.Combine(dir, sprintf "%s_rainbowifyed%s" name ext))
// provide your own test data!
rainbowifyBitmap @"c:\users\robert.RFQ-HUB.COM\Downloads\RobertPickering_FormalDress.jpg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.