/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSColor+Luminance.m

http://macfuse.googlecode.com/ · Objective C · 155 lines · 115 code · 16 blank · 24 comment · 4 complexity · 5026023d77b1afbf597f9f690ef05107 MD5 · raw file

  1. //
  2. // GTMNSColor+Luminance.m
  3. //
  4. // Copyright 2009 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMDefines.h"
  19. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  20. #import "GTMNSColor+Luminance.h"
  21. static const CGFloat kGTMLuminanceDarkCutoff = 0.6;
  22. @implementation NSColorSpace (GTMNSColorSpaceLuminanceHelpers)
  23. // TODO(alcor): we may want to keep one of these around for performance reasons
  24. + (NSColorSpace *)gtm_labColorSpace {
  25. // Observer= 2?, Illuminant= D65
  26. // TODO(alcor): these should come from ColorSync
  27. CGFloat whitePoint[3] = {0.95047, 1.0, 1.08883};
  28. CGFloat blackPoint[3] = {0, 0, 0};
  29. CGFloat range[4] = {-127, 127, -127, 127};
  30. CGColorSpaceRef cs = CGColorSpaceCreateLab(whitePoint, blackPoint, range);
  31. NSColorSpace *space = nil;
  32. if (cs) {
  33. space = [[[NSColorSpace alloc] initWithCGColorSpace:cs] autorelease];
  34. CGColorSpaceRelease(cs);
  35. }
  36. return space;
  37. }
  38. @end
  39. @implementation NSColor (GTMLuminance)
  40. - (NSColor *)labColor {
  41. return [self colorUsingColorSpace:[NSColorSpace gtm_labColorSpace]];
  42. }
  43. - (CGFloat)gtm_luminance {
  44. CGFloat lab[4];
  45. lab[0] = 0.0;
  46. [[self labColor] getComponents:lab];
  47. return lab[0] / 100.0;
  48. }
  49. - (NSColor *)gtm_colorByAdjustingLuminance:(CGFloat)luminance
  50. saturation:(CGFloat)saturation {
  51. CGFloat lab[4];
  52. [[self labColor] getComponents:lab];
  53. lab[0] *= 1.0 + luminance;
  54. // If luminance is greater than 100, we desaturate it so that we don't get
  55. // wild colors coming out of the forumula
  56. if (lab[0] > 100) {
  57. CGFloat clipping = lab[0] - 100;
  58. CGFloat desaturation = (50.0 - clipping) / 50.0;
  59. saturation = MIN(saturation, desaturation);
  60. }
  61. lab[1] *= saturation;
  62. lab[2] *= saturation;
  63. return [NSColor colorWithColorSpace:[NSColorSpace gtm_labColorSpace]
  64. components:lab
  65. count:sizeof(lab) / sizeof(lab[0])];
  66. }
  67. - (NSColor *)gtm_colorByAdjustingLuminance:(CGFloat)luminance {
  68. return [self gtm_colorByAdjustingLuminance:luminance saturation:1.0];
  69. }
  70. // TODO(alcor): these constants are largely made up, come up with a consistent
  71. // set of values or at least guidelines
  72. - (NSColor *)gtm_colorAdjustedFor:(GTMColorationUse)use {
  73. NSColor *color = nil;
  74. switch (use) {
  75. case GTMColorationBaseHighlight:
  76. color = [self gtm_colorByAdjustingLuminance:0.15];
  77. break;
  78. case GTMColorationBaseMidtone:
  79. color = self;
  80. break;
  81. case GTMColorationBaseShadow:
  82. color = [self gtm_colorByAdjustingLuminance:-0.15];
  83. break;
  84. case GTMColorationBasePenumbra:
  85. color = [self gtm_colorByAdjustingLuminance:-0.10];
  86. break;
  87. case GTMColorationLightHighlight:
  88. color = [self gtm_colorByAdjustingLuminance:0.25];
  89. color = [color blendedColorWithFraction:0.9 ofColor:[NSColor whiteColor]];
  90. break;
  91. case GTMColorationLightMidtone:
  92. color = [self blendedColorWithFraction:0.8 ofColor:[NSColor whiteColor]];
  93. break;
  94. case GTMColorationLightShadow:
  95. color = [self blendedColorWithFraction:0.7 ofColor:[NSColor whiteColor]];
  96. color = [color gtm_colorByAdjustingLuminance:-0.02];
  97. break;
  98. case GTMColorationLightPenumbra:
  99. color = [self blendedColorWithFraction:0.8 ofColor:[NSColor whiteColor]];
  100. color = [color gtm_colorByAdjustingLuminance:-0.01];
  101. break;
  102. case GTMColorationDarkHighlight:
  103. color = [self gtm_colorByAdjustingLuminance:-0.20];
  104. break;
  105. case GTMColorationDarkMidtone:
  106. color = [self gtm_colorByAdjustingLuminance:-0.25];
  107. break;
  108. case GTMColorationDarkShadow:
  109. color = [self gtm_colorByAdjustingLuminance:-0.30 saturation:1.4];
  110. break;
  111. case GTMColorationDarkPenumbra:
  112. color = [self gtm_colorByAdjustingLuminance:-0.25];
  113. break;
  114. default:
  115. _GTMDevLog(@"Invalid Coloration Use %lu", (unsigned long)use);
  116. color = self;
  117. break;
  118. }
  119. return color;
  120. }
  121. const CGFloat kDefaultFade = 0.3;
  122. - (NSColor *)gtm_colorAdjustedFor:(GTMColorationUse)use faded:(BOOL)fade {
  123. NSColor *color = [self gtm_colorAdjustedFor:use];
  124. if (fade) {
  125. CGFloat luminance = [color gtm_luminance];
  126. color = [color gtm_colorByAdjustingLuminance:
  127. kDefaultFade * (1.0 - luminance)
  128. saturation:kDefaultFade];
  129. }
  130. return color;
  131. }
  132. - (BOOL)gtm_isDarkColor {
  133. return [self gtm_luminance] < kGTMLuminanceDarkCutoff;
  134. }
  135. - (NSColor *)gtm_legibleTextColor {
  136. return [self gtm_isDarkColor] ? [NSColor whiteColor] : [NSColor blackColor];
  137. }
  138. @end
  139. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5