Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image picker support #31

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ See included example project (`TestColorViewController`).

## Requirements:

* Accelerate.framework
* QuartzCore.framework
* CoreGraphics.framework
* UIKit.framework
Expand Down
4 changes: 4 additions & 0 deletions RSColorPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
1FA35E7013F57B2800A6B347 /* RSColorPickerAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FA35E6F13F57B2800A6B347 /* RSColorPickerAppDelegate.m */; };
1FA35E9913F5A17E00A6B347 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA35E9813F5A17E00A6B347 /* QuartzCore.framework */; };
2B12A0DF1792FF46007F2A0A /* TestColorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B12A0DE1792FF46007F2A0A /* TestColorViewController.m */; };
2B3D0C1317A4254500D7FA54 /* test.png in Resources */ = {isa = PBXBuildFile; fileRef = 2B3D0C1217A4254500D7FA54 /* test.png */; };
2B85423116EFC8970047FFBE /* ANImageBitmapRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B85420D16EFC8970047FFBE /* ANImageBitmapRep.m */; };
2B85423216EFC8970047FFBE /* BitmapContextRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B85420F16EFC8970047FFBE /* BitmapContextRep.m */; };
2B85423316EFC8970047FFBE /* NSImage+ANImageBitmapRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B85421216EFC8970047FFBE /* NSImage+ANImageBitmapRep.m */; };
Expand Down Expand Up @@ -52,6 +53,7 @@
1FA35E9813F5A17E00A6B347 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
2B12A0DD1792FF46007F2A0A /* TestColorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestColorViewController.h; sourceTree = "<group>"; };
2B12A0DE1792FF46007F2A0A /* TestColorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestColorViewController.m; sourceTree = "<group>"; };
2B3D0C1217A4254500D7FA54 /* test.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = test.png; sourceTree = "<group>"; };
2B85420C16EFC8970047FFBE /* ANImageBitmapRep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANImageBitmapRep.h; sourceTree = "<group>"; };
2B85420D16EFC8970047FFBE /* ANImageBitmapRep.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ANImageBitmapRep.m; sourceTree = "<group>"; };
2B85420E16EFC8970047FFBE /* BitmapContextRep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BitmapContextRep.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -114,6 +116,7 @@
isa = PBXGroup;
children = (
930D5A7F16DE4D36006C38D4 /* [email protected] */,
2B3D0C1217A4254500D7FA54 /* test.png */,
1FA35E6513F57B2700A6B347 /* RSColorPicker */,
1FA35E5E13F57B2700A6B347 /* Frameworks */,
1FA35E5C13F57B2700A6B347 /* Products */,
Expand Down Expand Up @@ -302,6 +305,7 @@
files = (
1FA35E6A13F57B2800A6B347 /* InfoPlist.strings in Resources */,
930D5A8016DE4D36006C38D4 /* [email protected] in Resources */,
2B3D0C1317A4254500D7FA54 /* test.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion RSColorPicker/ColorPickerClasses/BGRSLoupeLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ @implementation BGRSLoupeLayer
@synthesize loupeCenter, colorPicker;

const CGFloat LOUPE_SIZE = 85, SHADOW_SIZE = 6, RIM_THICKNESS = 3.0;
const int NUM_PIXELS = 5, NUM_SKIP = 15;
const int NUM_PIXELS = 5, NUM_SKIP = 1;

- (id)init
{
Expand Down
2 changes: 2 additions & 0 deletions RSColorPicker/ColorPickerClasses/RSColorPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@property (nonatomic, weak) id <RSColorPickerViewDelegate> delegate;
@property (nonatomic, readonly) CGPoint selection;

@property (nonatomic) UIImage *customImage;

-(UIColor*)colorAtPoint:(CGPoint)point; // Returns UIColor at a point in the RSColorPickerView

+(void)prepareForDiameter:(CGFloat)diameter;
Expand Down
26 changes: 20 additions & 6 deletions RSColorPicker/ColorPickerClasses/RSColorPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,17 @@ -(void)didMoveToWindow {
- (void)genBitmap {
if (!_colorPickerViewFlags.bitmapNeedsUpdate) return;

CGFloat paddingDistance = _selectionView.bounds.size.width / 2.0;
_rep = [[self class] bitmapForDiameter:_gradientView.bounds.size.width scale:_scale padding:paddingDistance shouldCache:YES];
BMPoint newSize = BMPointFromSize(RSCGSizeWithScale(_gradientView.bounds.size, _scale));

if (_scale <= 0) {
_rep = [[ANImageBitmapRep alloc] initWithSize:newSize];
} else if (_customImage) {
_rep = [[ANImageBitmapRep alloc] initWithImage:_customImage];
[_rep setSizeFillingFrame:newSize];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be in the background

} else {
CGFloat paddingDistance = _selectionView.bounds.size.width / 2.0;
_rep = [[self class] bitmapForDiameter:_gradientView.bounds.size.width scale:_scale padding:paddingDistance shouldCache:YES];
}

_colorPickerViewFlags.bitmapNeedsUpdate = NO;
_gradientView.image = RSUIImageWithScale([_rep image], _scale);
Expand All @@ -173,14 +182,19 @@ - (UIColor*)colorAtPoint:(CGPoint)point {
if (convertedPoint.y >= _gradientContainer.bounds.size.height) convertedPoint.y = _gradientContainer.bounds.size.height - 1;

BMPixel pixel = [_rep getPixelAtPoint:BMPointFromPoint(RSCGPointWithScale(convertedPoint, _scale))];
UIColor *rgbColor = [UIColor colorWithRed:pixel.red green:pixel.green blue:pixel.blue alpha:1];
CGFloat h, s, v;
[rgbColor getHue:&h saturation:&s brightness:&v alpha:NULL];
return [UIColor colorWithHue:h saturation:s brightness:_brightness alpha:_opacity];
return [UIColor colorWithRed:pixel.red*_brightness green:pixel.green*_brightness blue:pixel.blue*_brightness alpha:_opacity];
}

#pragma mark - Setters

-(void)setCustomImage:(UIImage *)customImage {
if (customImage == _customImage) return;
_customImage = customImage;

_colorPickerViewFlags.bitmapNeedsUpdate = YES;
[self genBitmap];
}

- (void)setBrightness:(CGFloat)bright {
_brightness = bright;

Expand Down
3 changes: 2 additions & 1 deletion RSColorPicker/TestColorViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ - (void)viewDidLoad

// View that displays color picker (needs to be square)
_colorPicker = [[RSColorPickerView alloc] initWithFrame:CGRectMake(20.0, 10.0, 280.0, 280.0)];
[_colorPicker setCropToCircle:YES]; // Defaults to YES (and you can set BG color)
[_colorPicker setCropToCircle:NO]; // Defaults to YES (and you can set BG color)
// [_colorPicker setCustomImage:[UIImage imageNamed:@"test.png"]];
[_colorPicker setDelegate:self];
[self.view addSubview:_colorPicker];

Expand Down
Binary file added test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.