diff --git a/AppKit/CoreGraphics/CGGradient.j b/AppKit/CoreGraphics/CGGradient.j index e8396aa3d..24d0c3573 100644 --- a/AppKit/CoreGraphics/CGGradient.j +++ b/AppKit/CoreGraphics/CGGradient.j @@ -29,10 +29,15 @@ kCGGradientDrawsAfterEndLocation = 1 << 1; function CGGradientCreateWithColorComponents(aColorSpace, components, locations, count) { - if (locations === NULL) - var locations = [0.0, 1.0]; + if ( locations === undefined || locations === NULL ) + { + var num_of_colors = components.length / 4, + locations = []; + for ( var idx = 0; idx < num_of_colors; idx++ ) + locations.push( idx / (num_of_colors - 1) ); + } - if (count === NULL) + if ( count === undefined || count === NULL) var count = locations.length; var colors = []; diff --git a/Tests/AppKit/CGGradientTest.j b/Tests/AppKit/CGGradientTest.j index 187dd713d..0f89d0c60 100644 --- a/Tests/AppKit/CGGradientTest.j +++ b/Tests/AppKit/CGGradientTest.j @@ -7,6 +7,22 @@ */ - (void)testCreateWithColorComponentsDefault { + + // Test one color and no location + var components = [ 0.2,0.2,0.4,0.4 ]; + + var tmp = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), + components); + [self assert:1 equals:tmp.colors.length]; + [self assert:1 equals:tmp.locations.length]; + + // TODO: This should not be supported since there is no gradient if only one color + // TODO: NaN not really helpful... + [self assert:NaN equals:tmp.locations[0]]; + + [self assert:components.slice(0,4) equals:tmp.colors[0].components]; + + // Test two colors and no location var components = [ 0.2,0.2,0.4,0.4, 0.6,0.6,0.8,0.8 ]; @@ -20,6 +36,24 @@ [self assert:components.slice(4,8) equals:tmp.colors[1].components]; [self assert:components.slice(0,4) equals:tmp.colors[0].components]; + + // Test three colors and no location + var components = [ 0.2,0.2,0.4,0.4, + 0.6,0.6,0.8,0.8, + 0.6,0.6,0.8,0.8 ]; + + var tmp = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), + components); + [self assert:3 equals:tmp.colors.length]; + [self assert:3 equals:tmp.locations.length]; + + [self assert:0 equals:tmp.locations[0]]; + [self assert:0.5 equals:tmp.locations[1]]; + [self assert:1.0 equals:tmp.locations[2]]; + + [self assert:components.slice(8,12) equals:tmp.colors[2].components]; + [self assert:components.slice(4,8) equals:tmp.colors[1].components]; + [self assert:components.slice(0,4) equals:tmp.colors[0].components]; } /*