Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active June 7, 2017 18:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkanyildiz/2b0598daae9ffd19648d to your computer and use it in GitHub Desktop.
Save erkanyildiz/2b0598daae9ffd19648d to your computer and use it in GitHub Desktop.
Simple UIImage with digital camouflage patterns (Woodland, Snow, Desert, Naval, Savanna, Airborne, Ocean, Arid)
// erkanyildiz
// 20160620-1913
//
// 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
// erkanyildiz
// 20160620-1913
//
// 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
@erkanyildiz
Copy link
Author

Woodland
woodland
Snow
snow
Desert
desert
Naval
naval
Savanna
savanna
Airborne
airborne
Ocean
ocean
Arid
arid

@erkanyildiz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment