PageRenderTime 78ms CodeModel.GetById 18ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/AppKit/GTMLinearRGBShadingTest.m

http://macfuse.googlecode.com/
Objective C | 132 lines | 100 code | 8 blank | 24 comment | 3 complexity | 82e7027d55784b1db1d76b8680ea6a34 MD5 | raw file
  1//
  2//  GTMLinearRGBShadingTest.m
  3//
  4//  Copyright 2006-2008 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
 19#import <SenTestingKit/SenTestingKit.h>
 20#import "GTMSenTestCase.h"
 21#import "GTMLinearRGBShading.h"
 22
 23@interface GTMLinearRGBShadingTest : GTMTestCase
 24@end
 25
 26@implementation GTMLinearRGBShadingTest
 27- (void)testShadingFrom {
 28  // Create a shading from red to blue, and check if 50% is purple
 29  NSColor *red = [NSColor redColor];
 30  NSColor *blue = [NSColor blueColor];
 31  NSColor *purple = [NSColor purpleColor];
 32  GTMLinearRGBShading *theShading =
 33    [GTMLinearRGBShading shadingFromColor:red
 34                                  toColor:blue
 35                           fromSpaceNamed:NSCalibratedRGBColorSpace];
 36  STAssertNotNil(theShading,nil);
 37  STAssertEquals([theShading stopCount], (NSUInteger)2, nil);
 38  CGFloat *theColor = (CGFloat*)[theShading valueAtPosition: 0.5];
 39  STAssertEqualsWithAccuracy(theColor[0], [purple redComponent], 0.001, nil);
 40  STAssertEqualsWithAccuracy(theColor[1], [purple greenComponent], 0.001, nil);
 41  STAssertEqualsWithAccuracy(theColor[2], [purple blueComponent], 0.001, nil);
 42  STAssertEqualsWithAccuracy(theColor[3], [purple alphaComponent], 0.001, nil);
 43}
 44
 45- (void)testShadingWith {
 46  // Create a shading with kColorCount colors and make sure all the values are there.
 47  enum { kColorCount = 100 };
 48  NSColor *theColors[kColorCount];
 49  CGFloat thePositions[kColorCount];
 50  const CGFloat kColorIncrement = 1.0 / kColorCount;
 51  for (NSUInteger i = 0; i < kColorCount; i++) {
 52    CGFloat newValue = kColorIncrement * i;
 53    thePositions[i] = newValue;
 54    theColors[i] = [NSColor colorWithCalibratedRed:newValue 
 55                                             green:newValue 
 56                                              blue:newValue 
 57                                             alpha:newValue];
 58  }
 59  GTMLinearRGBShading *theShading =
 60    [GTMLinearRGBShading shadingWithColors:theColors
 61                            fromSpaceNamed:NSCalibratedRGBColorSpace
 62                               atPositions:thePositions
 63                                     count:kColorCount];
 64  for (NSUInteger i = 0; i < kColorCount; i++) {
 65    CGFloat newValue = kColorIncrement * i;
 66    CGFloat *theColor = (CGFloat*)[theShading valueAtPosition:newValue];
 67    STAssertEqualsWithAccuracy(theColor[0], newValue, 0.001, nil);
 68    STAssertEqualsWithAccuracy(theColor[1], newValue, 0.001, nil);
 69    STAssertEqualsWithAccuracy(theColor[2], newValue, 0.001, nil);
 70    STAssertEqualsWithAccuracy(theColor[3], newValue, 0.001, nil);
 71  }
 72  // Create a shading with 1 color to test that special handling
 73  NSColor *purple = [NSColor purpleColor];
 74  NSColor *singleColor[1] = { purple };
 75  CGFloat singlePosition[1] = { 0.5 };
 76  theShading =
 77    [GTMLinearRGBShading shadingWithColors:singleColor
 78                            fromSpaceNamed:NSCalibratedRGBColorSpace
 79                               atPositions:singlePosition
 80                                     count:1];
 81  // test over a range to make sure we always get the same color
 82  for (NSUInteger i = 0; i < kColorCount; i++) {
 83    CGFloat newValue = kColorIncrement * i;
 84    CGFloat *theColor = (CGFloat*)[theShading valueAtPosition:newValue];
 85    STAssertEqualsWithAccuracy(theColor[0], [purple redComponent], 0.001, nil);
 86    STAssertEqualsWithAccuracy(theColor[1], [purple greenComponent], 0.001, nil);
 87    STAssertEqualsWithAccuracy(theColor[2], [purple blueComponent], 0.001, nil);
 88    STAssertEqualsWithAccuracy(theColor[3], [purple alphaComponent], 0.001, nil);
 89  }
 90}
 91
 92- (void)testShadeFunction {
 93  GTMLinearRGBShading *theShading =
 94    [GTMLinearRGBShading shadingWithColors:nil
 95                            fromSpaceNamed:NSCalibratedRGBColorSpace
 96                               atPositions:nil
 97                                     count:0];
 98  CGFunctionRef theFunction = [theShading shadeFunction];
 99  STAssertNotNULL(theFunction, nil);
100  STAssertEquals(CFGetTypeID(theFunction), CGFunctionGetTypeID(), nil);  
101}
102
103- (void)testColorSpace {
104  // Calibrated RGB
105  GTMLinearRGBShading *theShading =
106    [GTMLinearRGBShading shadingWithColors:nil
107                            fromSpaceNamed:NSCalibratedRGBColorSpace
108                               atPositions:nil
109                                     count:0];
110  CGColorSpaceRef theColorSpace = [theShading colorSpace];
111  STAssertNotNULL(theColorSpace, nil);
112  STAssertEquals(CFGetTypeID(theColorSpace), CGColorSpaceGetTypeID(), nil);
113
114  // Device RGB
115  theShading =
116    [GTMLinearRGBShading shadingWithColors:nil
117                            fromSpaceNamed:NSDeviceRGBColorSpace
118                               atPositions:nil
119                                     count:0];
120  theColorSpace = [theShading colorSpace];
121  STAssertNotNULL(theColorSpace, nil);
122  STAssertEquals(CFGetTypeID(theColorSpace), CGColorSpaceGetTypeID(), nil);
123  
124  // Device CMYK (not supported)
125  theShading =
126    [GTMLinearRGBShading shadingWithColors:nil
127                            fromSpaceNamed:NSDeviceCMYKColorSpace
128                               atPositions:nil
129                                     count:0];
130  STAssertNULL(theShading, nil);
131}
132@end