Colours.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // Copyright (C) 2013 by Benjamin Gordon
  2. //
  3. // Permission is hereby granted, free of charge, to any
  4. // person obtaining a copy of this software and
  5. // associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge,
  8. // publish, distribute, sublicense, and/or sell copies of the
  9. // Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall
  13. // be included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #include "TargetConditionals.h"
  23. #include <Foundation/Foundation.h>
  24. #pragma mark - Static String Keys
  25. static NSString * kColoursRGBA_R = @"RGBA-r";
  26. static NSString * kColoursRGBA_G = @"RGBA-g";
  27. static NSString * kColoursRGBA_B = @"RGBA-b";
  28. static NSString * kColoursRGBA_A = @"RGBA-a";
  29. static NSString * kColoursHSBA_H = @"HSBA-h";
  30. static NSString * kColoursHSBA_S = @"HSBA-s";
  31. static NSString * kColoursHSBA_B = @"HSBA-b";
  32. static NSString * kColoursHSBA_A = @"HSBA-a";
  33. static NSString * kColoursCIE_L = @"LABa-L";
  34. static NSString * kColoursCIE_A = @"LABa-A";
  35. static NSString * kColoursCIE_B = @"LABa-B";
  36. static NSString * kColoursCIE_alpha = @"LABa-a";
  37. static NSString * kColoursCIE_C = @"LABa-C";
  38. static NSString * kColoursCIE_H = @"LABa-H";
  39. static NSString * kColoursCMYK_C = @"CMYK-c";
  40. static NSString * kColoursCMYK_M = @"CMYK-m";
  41. static NSString * kColoursCMYK_Y = @"CMYK-y";
  42. static NSString * kColoursCMYK_K = @"CMYK-k";
  43. #pragma mark - Create correct iOS/OSX interface
  44. #if TARGET_OS_IPHONE
  45. #import <UIKit/UIKit.h>
  46. @interface UIColor (Colours)
  47. #elif TARGET_OS_MAC
  48. #import <AppKit/AppKit.h>
  49. @interface NSColor (Colours)
  50. #endif
  51. #pragma mark - Enums
  52. // Color Scheme Generation Enum
  53. typedef NS_ENUM(NSInteger, ColorScheme) {
  54. ColorSchemeAnalagous,
  55. ColorSchemeMonochromatic,
  56. ColorSchemeTriad,
  57. ColorSchemeComplementary
  58. };
  59. // ColorFormulation Type
  60. typedef NS_ENUM(NSInteger, ColorFormulation) {
  61. ColorFormulationRGBA,
  62. ColorFormulationHSBA,
  63. ColorFormulationLAB,
  64. ColorFormulationCMYK
  65. };
  66. // ColorDistance
  67. typedef NS_ENUM(NSInteger, ColorDistance) {
  68. ColorDistanceCIE76,
  69. ColorDistanceCIE94,
  70. ColorDistanceCIE2000,
  71. };
  72. typedef NS_ENUM(NSInteger, ColorComparison) {
  73. ColorComparisonDarkness,
  74. ColorComparisonLightness,
  75. ColorComparisonDesaturated,
  76. ColorComparisonSaturated,
  77. ColorComparisonRed,
  78. ColorComparisonGreen,
  79. ColorComparisonBlue
  80. };
  81. #pragma mark - Color from Hex/RGBA/HSBA/CIE_LAB/CMYK
  82. /**
  83. Creates a Color from a Hex representation string
  84. @param hexString Hex string that looks like @"#FF0000" or @"FF0000"
  85. @return Color
  86. */
  87. + (instancetype)colorFromHexString:(NSString *)hexString;
  88. /**
  89. Creates a Color from an array of 4 NSNumbers (r,g,b,a)
  90. @param rgbaArray 4 NSNumbers for rgba between 0 - 1
  91. @return Color
  92. */
  93. + (instancetype)colorFromRGBAArray:(NSArray *)rgbaArray;
  94. /**
  95. Creates a Color from a dictionary of 4 NSNumbers
  96. Keys: kColoursRGBA_R, kColoursRGBA_G, kColoursRGBA_B, kColoursRGBA_A
  97. @param rgbaDictionary 4 NSNumbers for rgba between 0 - 1
  98. @return Color
  99. */
  100. + (instancetype)colorFromRGBADictionary:(NSDictionary *)rgbaDict;
  101. /**
  102. Creates a Color from an array of 4 NSNumbers (h,s,b,a)
  103. @param hsbaArray 4 NSNumbers for rgba between 0 - 1
  104. @return Color
  105. */
  106. + (instancetype)colorFromHSBAArray:(NSArray *)hsbaArray;
  107. /**
  108. Creates a Color from a dictionary of 4 NSNumbers
  109. Keys: kColoursHSBA_H, kColoursHSBA_S, kColoursHSBA_B, kColoursHSBA_A
  110. @param hsbaDictionary 4 NSNumbers for rgba between 0 - 1
  111. @return Color
  112. */
  113. + (instancetype)colorFromHSBADictionary:(NSDictionary *)hsbaDict;
  114. /**
  115. Creates a Color from an array of 4 NSNumbers (L,a,b,alpha)
  116. @param colors 4 NSNumbers for CIE_LAB between 0 - 1
  117. @return Color
  118. */
  119. + (instancetype)colorFromCIE_LabArray:(NSArray *)colors;
  120. /**
  121. Creates a Color from a dictionary of 4 NSNumbers
  122. Keys: kColoursCIE_L, kColoursCIE_A, kColoursCIE_B, kColoursCIE_alpha
  123. @param colors 4 NSNumbers for CIE_LAB between 0 - 1
  124. @return Color
  125. */
  126. + (instancetype)colorFromCIE_LabDictionary:(NSDictionary *)colors;
  127. /**
  128. Creates a Color from an array of 4 NSNumbers (L,a,b,alpha)
  129. @param colors 4 NSNumbers for CIE_LAB between 0 - 1
  130. @return Color
  131. */
  132. + (instancetype)colorFromCIE_LCHArray:(NSArray *)colors;
  133. /**
  134. Creates a Color from a dictionary of 4 NSNumbers
  135. Keys: kColoursCIE_L, kColoursCIE_A, kColoursCIE_B, kColoursCIE_alpha
  136. @param colors 4 NSNumbers for CIE_LAB between 0 - 1
  137. @return Color
  138. */
  139. + (instancetype)colorFromCIE_LCHDictionary:(NSDictionary *)colors;
  140. /**
  141. Creates a Color from an array of 4 NSNumbers (C,M,Y,K)
  142. @param colors 4 NSNumbers for CMYK between 0 - 1
  143. @return Color
  144. */
  145. + (instancetype)colorFromCMYKArray:(NSArray *)cmyk;
  146. /**
  147. Creates a Color from a dictionary of 4 NSNumbers
  148. Keys: kColoursCMYK_C, kColoursCMYK_M, kColoursCMYK_Y, kColoursCMYK_K
  149. @param colors 4 NSNumbers for CMYK between 0 - 1
  150. @return Color
  151. */
  152. + (instancetype)colorFromCMYKDictionary:(NSDictionary *)cmyk;
  153. #pragma mark - Hex/RGBA/HSBA/CIE_LAB/CMYK from Color
  154. /**
  155. Creates a Hex representation from a Color
  156. @return NSString
  157. */
  158. - (NSString *)hexString;
  159. /**
  160. Creates an array of 4 NSNumbers representing the float values of r, g, b, a in that order.
  161. @return NSArray
  162. */
  163. - (NSArray *)rgbaArray;
  164. /**
  165. Creates an array of 4 NSNumbers representing the float values of h, s, b, a in that order.
  166. @return NSArray
  167. */
  168. - (NSArray *)hsbaArray;
  169. /**
  170. Creates a dictionary of 4 NSNumbers representing float values with keys: kColoursRGBA_R, kColoursRGBA_G, kColoursRGBA_B, kColoursRGBA_A
  171. @return NSDictionary
  172. */
  173. - (NSDictionary *)rgbaDictionary;
  174. /**
  175. Creates a dictionary of 4 NSNumbers representing float values with keys: kColoursHSBA_H, kColoursHSBA_S, kColoursHSBA_B, kColoursHSBA_A
  176. @return NSDictionary
  177. */
  178. - (NSDictionary *)hsbaDictionary;
  179. /**
  180. * Creates an array of 4 NSNumbers representing the float values of L*, a, b, alpha in that order.
  181. *
  182. * @return NSArray
  183. */
  184. - (NSArray *)CIE_LabArray;
  185. /**
  186. * Creates a dictionary of 4 NSNumbers representing the float values with keys: kColoursCIE_L, kColoursCIE_A, kColoursCIE_B, kColoursCIE_alpha
  187. *
  188. * @return NSDictionary
  189. */
  190. - (NSDictionary *)CIE_LabDictionary;
  191. /**
  192. * Creates an array of 4 NSNumbers representing the float values of L*, a, b, alpha in that order.
  193. *
  194. * @return NSArray
  195. */
  196. - (NSArray*)CIE_LCHArray;
  197. /**
  198. * Creates a dictionary of 4 NSNumbers representing the float values with keys: kColoursCIE_L, kColoursCIE_A, kColoursCIE_B, kColoursCIE_alpha
  199. *
  200. * @return NSDictionary
  201. */
  202. - (NSDictionary *)CIE_LCHDictionary;
  203. /**
  204. * Creates an array of 4 NSNumbers representing the float values of C, M, Y, K in that order.
  205. *
  206. * @return NSArray
  207. */
  208. - (NSArray *)cmykArray;
  209. /**
  210. * Creates a dictionary of 4 NSNumbers representing the float values with keys: kColoursCMYK_C, kColoursCMYK_M, kColoursCMYK_Y, kColoursCMYK_K
  211. *
  212. * @return NSDictionary
  213. */
  214. - (NSDictionary *)cmykDictionary;
  215. #pragma mark - Color Components
  216. /**
  217. * Creates an NSDictionary with RGBA and HSBA color components inside.
  218. *
  219. * @return NSDictionary
  220. */
  221. - (NSDictionary *)colorComponents;
  222. /**
  223. * Returns the red value from an RGBA formulation of the UIColor.
  224. *
  225. * @return CGFloat
  226. */
  227. - (CGFloat)red;
  228. /**
  229. * Returns the green value from an RGBA formulation of the UIColor.
  230. *
  231. * @return CGFloat
  232. */
  233. - (CGFloat)green;
  234. /**
  235. * Returns the blue value from an RGBA formulation of the UIColor.
  236. *
  237. * @return CGFloat
  238. */
  239. - (CGFloat)blue;
  240. /**
  241. * Returns the hue value from an HSBA formulation of the UIColor.
  242. *
  243. * @return CGFloat
  244. */
  245. - (CGFloat)hue;
  246. /**
  247. * Returns the saturation value from an HSBA formulation of the UIColor.
  248. *
  249. * @return CGFloat
  250. */
  251. - (CGFloat)saturation;
  252. /**
  253. * Returns the brightness value from an HSBA formulation of the UIColor.
  254. *
  255. * @return CGFloat
  256. */
  257. - (CGFloat)brightness;
  258. /**
  259. * Returns the alpha value from an RGBA formulation of the UIColor.
  260. *
  261. * @return CGFloat
  262. */
  263. - (CGFloat)alpha;
  264. /**
  265. * Returns the lightness value from a CIELAB formulation of the UIColor.
  266. *
  267. * @return CGFloat
  268. */
  269. - (CGFloat)CIE_Lightness;
  270. /**
  271. * Returns the a value from a CIELAB formulation of the UIColor.
  272. *
  273. * @return CGFloat
  274. */
  275. - (CGFloat)CIE_a;
  276. /**
  277. * Returns the b value from a CIELAB formulation of the UIColor.
  278. *
  279. * @return CGFloat
  280. */
  281. - (CGFloat)CIE_b;
  282. /**
  283. * Returns the cyan value from a CMYK formulation of the UIColor.
  284. *
  285. * @return CGFloat
  286. */
  287. - (CGFloat)cyan;
  288. /**
  289. * Returns the magenta value from a CMYK formulation of the UIColor.
  290. *
  291. * @return CGFloat
  292. */
  293. - (CGFloat)magenta;
  294. /**
  295. * Returns the yellow value from a CMYK formulation of the UIColor.
  296. *
  297. * @return CGFloat
  298. */
  299. - (CGFloat)yellow;
  300. /**
  301. * Returns the black (K) value from a CMYK formulation of the UIColor.
  302. *
  303. * @return CGFloat
  304. */
  305. - (CGFloat)keyBlack;
  306. #pragma mark - Darken/Lighten
  307. /**
  308. * Darkens a color by changing the brightness by a percentage you pass in. If you want a 25% darker color, you pass in 0.25;
  309. *
  310. * @param percentage CGFloat
  311. *
  312. * @return Color
  313. */
  314. - (instancetype)darken:(CGFloat)percentage;
  315. /**
  316. * Lightens a color by changing the brightness by a percentage you pass in. If you want a 25% lighter color, you pass in 0.25;
  317. *
  318. * @param percentage CGFloat
  319. *
  320. * @return Color
  321. */
  322. - (instancetype)lighten:(CGFloat)percentage;
  323. #pragma mark - 4 Color Scheme from Color
  324. /**
  325. Creates an NSArray of 4 Colors that complement the Color.
  326. @param type ColorSchemeAnalagous, ColorSchemeMonochromatic, ColorSchemeTriad, ColorSchemeComplementary
  327. @return NSArray
  328. */
  329. - (NSArray *)colorSchemeOfType:(ColorScheme)type;
  330. #pragma mark - Contrasting Color from Color
  331. /**
  332. Creates either [Color whiteColor] or [Color blackColor] depending on if the color this method is run on is dark or light.
  333. @return Color
  334. */
  335. - (instancetype)blackOrWhiteContrastingColor;
  336. #pragma mark - Complementary Color
  337. /**
  338. Creates a complementary color - a color directly opposite it on the color wheel.
  339. @return Color
  340. */
  341. - (instancetype)complementaryColor;
  342. #pragma mark - Distance between Colors
  343. /**
  344. * Returns a float of the distance between 2 colors. Defaults to the
  345. * CIE94 specification found here: http://en.wikipedia.org/wiki/Color_difference
  346. *
  347. * @param color Color to check self with.
  348. *
  349. * @return CGFloat
  350. */
  351. - (CGFloat)distanceFromColor:(id)color;
  352. /**
  353. * Returns a float of the distance between 2 colors, using one of
  354. *
  355. *
  356. * @param color Color to check against
  357. * @param distanceType Formula to calculate with
  358. *
  359. * @return CGFloat
  360. */
  361. - (CGFloat)distanceFromColor:(id)color type:(ColorDistance)distanceType;
  362. #pragma mark - Compare Colors
  363. + (NSArray *)sortColors:(NSArray *)colors withComparison:(ColorComparison)comparison;
  364. + (NSComparisonResult)compareColor:(id)colorA andColor:(id)colorB withComparison:(ColorComparison)comparison;
  365. #pragma mark - Colors
  366. // System Colors
  367. + (instancetype)infoBlueColor;
  368. + (instancetype)successColor;
  369. + (instancetype)warningColor;
  370. + (instancetype)dangerColor;
  371. // Whites
  372. + (instancetype)antiqueWhiteColor;
  373. + (instancetype)oldLaceColor;
  374. + (instancetype)ivoryColor;
  375. + (instancetype)seashellColor;
  376. + (instancetype)ghostWhiteColor;
  377. + (instancetype)snowColor;
  378. + (instancetype)linenColor;
  379. // Grays
  380. + (instancetype)black25PercentColor;
  381. + (instancetype)black50PercentColor;
  382. + (instancetype)black75PercentColor;
  383. + (instancetype)warmGrayColor;
  384. + (instancetype)coolGrayColor;
  385. + (instancetype)charcoalColor;
  386. // Blues
  387. + (instancetype)tealColor;
  388. + (instancetype)steelBlueColor;
  389. + (instancetype)robinEggColor;
  390. + (instancetype)pastelBlueColor;
  391. + (instancetype)turquoiseColor;
  392. + (instancetype)skyBlueColor;
  393. + (instancetype)indigoColor;
  394. + (instancetype)denimColor;
  395. + (instancetype)blueberryColor;
  396. + (instancetype)cornflowerColor;
  397. + (instancetype)babyBlueColor;
  398. + (instancetype)midnightBlueColor;
  399. + (instancetype)fadedBlueColor;
  400. + (instancetype)icebergColor;
  401. + (instancetype)waveColor;
  402. // Greens
  403. + (instancetype)emeraldColor;
  404. + (instancetype)grassColor;
  405. + (instancetype)pastelGreenColor;
  406. + (instancetype)seafoamColor;
  407. + (instancetype)paleGreenColor;
  408. + (instancetype)cactusGreenColor;
  409. + (instancetype)chartreuseColor;
  410. + (instancetype)hollyGreenColor;
  411. + (instancetype)oliveColor;
  412. + (instancetype)oliveDrabColor;
  413. + (instancetype)moneyGreenColor;
  414. + (instancetype)honeydewColor;
  415. + (instancetype)limeColor;
  416. + (instancetype)cardTableColor;
  417. // Reds
  418. + (instancetype)salmonColor;
  419. + (instancetype)brickRedColor;
  420. + (instancetype)easterPinkColor;
  421. + (instancetype)grapefruitColor;
  422. + (instancetype)pinkColor;
  423. + (instancetype)indianRedColor;
  424. + (instancetype)strawberryColor;
  425. + (instancetype)coralColor;
  426. + (instancetype)maroonColor;
  427. + (instancetype)watermelonColor;
  428. + (instancetype)tomatoColor;
  429. + (instancetype)pinkLipstickColor;
  430. + (instancetype)paleRoseColor;
  431. + (instancetype)crimsonColor;
  432. // Purples
  433. + (instancetype)eggplantColor;
  434. + (instancetype)pastelPurpleColor;
  435. + (instancetype)palePurpleColor;
  436. + (instancetype)coolPurpleColor;
  437. + (instancetype)violetColor;
  438. + (instancetype)plumColor;
  439. + (instancetype)lavenderColor;
  440. + (instancetype)raspberryColor;
  441. + (instancetype)fuschiaColor;
  442. + (instancetype)grapeColor;
  443. + (instancetype)periwinkleColor;
  444. + (instancetype)orchidColor;
  445. // Yellows
  446. + (instancetype)goldenrodColor;
  447. + (instancetype)yellowGreenColor;
  448. + (instancetype)bananaColor;
  449. + (instancetype)mustardColor;
  450. + (instancetype)buttermilkColor;
  451. + (instancetype)goldColor;
  452. + (instancetype)creamColor;
  453. + (instancetype)lightCreamColor;
  454. + (instancetype)wheatColor;
  455. + (instancetype)beigeColor;
  456. // Oranges
  457. + (instancetype)peachColor;
  458. + (instancetype)burntOrangeColor;
  459. + (instancetype)pastelOrangeColor;
  460. + (instancetype)cantaloupeColor;
  461. + (instancetype)carrotColor;
  462. + (instancetype)mandarinColor;
  463. // Browns
  464. + (instancetype)chiliPowderColor;
  465. + (instancetype)burntSiennaColor;
  466. + (instancetype)chocolateColor;
  467. + (instancetype)coffeeColor;
  468. + (instancetype)cinnamonColor;
  469. + (instancetype)almondColor;
  470. + (instancetype)eggshellColor;
  471. + (instancetype)sandColor;
  472. + (instancetype)mudColor;
  473. + (instancetype)siennaColor;
  474. + (instancetype)dustColor;
  475. @end