Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active June 7, 2017 18:26

Revisions

  1. erkanyildiz revised this gist Jun 20, 2016. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions EYDigitalCamouflageImage.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // erkanyildiz
    // 20160223-1716
    // 20160620-1913
    //
    // EYDigitalCamouflageImage.h

    @@ -19,5 +19,5 @@ typedef enum:NSInteger
    } EYDigitalCamouflageImagePattern;

    @interface EYDigitalCamouflageImage : UIImage
    + (UIImage*)imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern;
    + (UIImage *)imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern;
    @end
    4 changes: 2 additions & 2 deletions EYDigitalCamouflageImage.m
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    // erkanyildiz
    // 20160223-1716
    // 20160620-1913
    //
    // EYDigitalCamouflageImage.m

    #import "EYDigitalCamouflageImage.h"

    @implementation EYDigitalCamouflageImage
    + (UIImage*) imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern
    + (UIImage *)imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern
    {
    const int numberOfPixels = 40; // number of pixels on the short side of the image
    const int splashMinimum = 5; // minimum number of pixels in a splash
  2. erkanyildiz revised this gist Feb 23, 2016. No changes.
  3. erkanyildiz created this gist Feb 23, 2016.
    23 changes: 23 additions & 0 deletions EYDigitalCamouflageImage.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // erkanyildiz
    // 20160223-1716
    //
    // EYDigitalCamouflageImage.h

    #import <UIKit/UIKit.h>

    typedef enum:NSInteger
    {
    EYDCIWoodland,
    EYDCISnow,
    EYDCIDesert,
    EYDCINaval,
    EYDCISavanna,
    EYDCIAirborne,
    EYDCIOcean,
    EYDCIArid,
    EYDigitalCamouflageImagePatternCount
    } EYDigitalCamouflageImagePattern;

    @interface EYDigitalCamouflageImage : UIImage
    + (UIImage*)imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern;
    @end
    111 changes: 111 additions & 0 deletions EYDigitalCamouflageImage.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    // erkanyildiz
    // 20160223-1716
    //
    // EYDigitalCamouflageImage.m

    #import "EYDigitalCamouflageImage.h"

    @implementation EYDigitalCamouflageImage
    + (UIImage*) imageWithSize:(CGSize)size andPattern:(EYDigitalCamouflageImagePattern)pattern
    {
    const int numberOfPixels = 40; // number of pixels on the short side of the image
    const int splashMinimum = 5; // minimum number of pixels in a splash
    const int splashMaximum = 20; // maximum number of pixels in a splash
    const float reducer = 0.08; // 0.08 is the magic number

    int width = size.width;
    int height = size.height;
    int pixelSize = MIN(width, height) / numberOfPixels;
    int numberOfSplashes = reducer * (width * height / (pixelSize * pixelSize));

    NSArray* colors = [self.class patternColors][pattern % EYDigitalCamouflageImagePatternCount];

    UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect bgRect = (CGRect){0, 0, size.width, size.height};
    [(UIColor*)colors[0] set];
    CGContextFillRect(context, bgRect); // draw background

    for (int i = 0; i < numberOfSplashes; i++)
    {
    int x = arc4random() % (width / pixelSize); // random start of splash, x
    int y = arc4random() % (height / pixelSize); // random start of splash, y
    int c = arc4random() % (colors.count-1) +1; // randomly chosen splash color (except background color)

    [(UIColor*)colors[c] set]; // set pixel color

    int splashPixelCount = (int) splashMinimum + arc4random() % (splashMaximum-splashMinimum);
    splashPixelCount *= (colors.count-c); // color balancing proportional to color index

    for (int j = 0; j < splashPixelCount; j++)
    {
    int dX = (arc4random() % 3) -1; // random direction of splash, x
    int dY = (arc4random() % 3) -1; // random direction of splash, y

    x = x + dX;
    y = y + dY;

    CGRect pixelRect = (CGRect){x * pixelSize, y * pixelSize, pixelSize, pixelSize};
    CGContextFillRect(context, pixelRect); // draw pixel
    }
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
    }


    + (NSArray*)patternColors
    {
    #define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

    // colors for patterns are ordered from most used (background color) to least used
    NSArray* colors = @[
    @[ RGB(156,136,109), //EYDCIWoodland
    RGB(88,109,89),
    RGB(57,57,59),
    RGB(165,157,142)
    ],
    @[ RGB(255,255,255), //EYDCISnow
    RGB(151,153,152),
    RGB(112, 129,136),
    RGB(78,85,77)
    ],
    @[ RGB(214,186,146), //EYDCIDesert
    RGB(234,196,169),
    RGB(241,214,194),
    RGB(148,104,70)
    ],
    @[ RGB(165,179,193), //EYDCINaval
    RGB(86,108,117),
    RGB(44,65,91),
    RGB(31,35,40)
    ],
    @[ RGB(100,133,64), //EYDCISavanna
    RGB(42,82,11),
    RGB(210,130,7),
    RGB(92,18,7)
    ],
    @[ RGB(198,205,197), //EYDCIAirborne
    RGB(246,235,218),
    RGB(103,118,111),
    RGB(75,93,97)
    ],
    @[ RGB(71,179,169), //EYDCIOcean
    RGB(0,124,73),
    RGB(255,255,255),
    RGB(0,0,0)
    ],
    @[ RGB(238,225,203), //EYDCIArid
    RGB(119,139,98),
    RGB(191,104,73),
    RGB(70,68,69)
    ]
    ];
    return colors;
    }

    @end