色盘取出点击处的R,G,B色值(

色盘取出点击处的颜色组成并显示在控制器的背景上。

效果图:

 

下面为核心源代码。

色盘的两个类i:

 

//  Created by dev on 16/2/4.
//  Copyright © 2016年 DLS. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DSColor.h"
@protocol DSColorDiskViewDelegate<NSObject>
@optional
-(void)selectCurrentColor:(DSColor *)currentColor;
@end

@interface DSColorDiskView : UIView
@property(nonatomic, strong)id<DSColorDiskViewDelegate>delegate;
@property(nonatomic, strong)DSColor *currentColor;
@end

 

//
//  DSColorDiskView.m
//  色盘取色
//
//  Created by dev on 16/2/4.
//  Copyright © 2016年 DLS. All rights reserved.
//

#import "DSColorDiskView.h"
#import "UIView+XMGExtension.h"
#import "Masonry.h"
#define radius self.diskImageView.frame.size.height/2-10
@interface DSColorDiskView()
@property(nonatomic, strong)UIImageView *diskImageView;
@property(nonatomic, strong)UIImage *diskImage;
@property(nonatomic, strong)UIImage *pointImage;
@property(nonatomic, strong)UIImageView *pointImageView;
@property(nonatomic, assign)CGPoint pp;
@end
@implementation DSColorDiskView
-(DSColor *)currentColor{
    if (!_currentColor) {
        _currentColor = [[DSColor alloc] init];
    }
    return _currentColor;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        
    }
    return self;
}
-(void)initView{
    _diskImage = [UIImage imageNamed:@"colorPicker5"];
    UIImageView *diskImageView = [[UIImageView alloc] initWithImage:_diskImage];
    
    self.diskImageView = diskImageView;
    [self addSubview:diskImageView];
    
    self.pointImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"colorPickerKnob"]];
    self.pointImageView.width = 30;
    self.pointImageView.height = 30;
    //self.pointImageView.backgroundColor = [UIColor redColor];
    [self.diskImageView addSubview:self.pointImageView];
    
}
/**
 *  布局时调用
 */
-(void)layoutSubviews{
    CGFloat Width = self.size.width;
    CGFloat Height = self.size.height;
    NSLog(@"Width = %f  Height = %f",Height,Width);
    [self.diskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(Height);
        make.width.mas_equalTo(Width);
    }];
    [self.pointImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
}
//可不用
-(void)setPp:(CGPoint )pp{
    _pp = pp;
    self.pointImageView.center = CGPointMake(_pp.x, _pp.y);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.diskImageView];
    
    double distance = sqrt(((self.diskImageView.center.x-point.x)*(self.diskImageView.center.x -point.x)) + ((self.diskImageView.center.y - point.y)*(self.diskImageView.center.y - point.y)));
    NSLog(@"radius = %f,distance = %f",radius,distance);
    if (distance > radius) {
        return;
    }
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height), point)) {
        return ;
    }
    //self.pp = point;
    self.pointImageView.center = CGPointMake(point.x, point.y);
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.diskImage.CGImage;
    NSUInteger width = self.frame.size.width;
    NSUInteger height = self.frame.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);
    
    // Convert color values [0..255] to floats [0.0..1.0]
    self.currentColor.R   = (CGFloat)pixelData[0] / 255.0f;
    self.currentColor.G  = (CGFloat)pixelData[1] / 255.0f;
    self.currentColor.B   = (CGFloat)pixelData[2] / 255.0f;
    NSLog(@"%f%f%f",(CGFloat)pixelData[0],(CGFloat)pixelData[1],(CGFloat)pixelData[2]);
    
    NSLog(@"%f",self.currentColor.R);
    [self.delegate selectCurrentColor:self.currentColor];
 }
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.diskImageView];
    
    double distance = sqrt(((self.diskImageView.center.x-point.x)*(self.diskImageView.center.x -point.x)) + ((self.diskImageView.center.y - point.y)*(self.diskImageView.center.y - point.y)));
    if (distance > radius) {
        return;
    }
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height), point)) {
        return ;
    }
    //self.pp = point;
    //中间小圆的位置
    self.pointImageView.center = CGPointMake(point.x, point.y);
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.diskImage.CGImage;
    NSUInteger width = self.frame.size.width;
    NSUInteger height = self.frame.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    //
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);
    
    //
    self.currentColor.R   = (CGFloat)pixelData[0] / 255.0f;
    self.currentColor.G  = (CGFloat)pixelData[1] / 255.0f;
    self.currentColor.B   = (CGFloat)pixelData[2] / 255.0f;
    NSLog(@"%f%f%f",(CGFloat)pixelData[0],(CGFloat)pixelData[1],(CGFloat)pixelData[2]);
    
    NSLog(@"%f",self.currentColor.R);
    [self.delegate selectCurrentColor:self.currentColor];
    //不通过代理直接设置父视图的背景颜色
    //self.superview.backgroundColor = [UIColor redColor];

}
@end
//
//  DSColor.h
//  色盘取色
//
//  Created by dev on 16/2/4.
//  Copyright © 2016年 DLS. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DSColor : NSObject
@property(nonatomic, assign)CGFloat R;
@property(nonatomic, assign)CGFloat G;
@property(nonatomic, assign)CGFloat B;

@end
//
//  DSColor.m
//  色盘取色
//
//  Created by dev on 16/2/4.
//  Copyright © 2016年 DLS. All rights reserved.
//

#import "DSColor.h"

@implementation DSColor

@end

下面为控制器代码:

#import "ViewController.h"
#import "DSColorDiskView.h"
#import "Masonry.h"
@interface ViewController ()<DSColorDiskViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    DSColorDiskView *colorDiskView = [[DSColorDiskView alloc] init];
    colorDiskView.delegate = self;
    [self.view addSubview:colorDiskView];
    //colorDiskView.backgroundColor = [UIColor redColor];
    [colorDiskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY).offset(-50);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(200);
    }];
}
-(void)selectCurrentColor:(DSColor *)currentColor{
    
    self.view.backgroundColor = [UIColor colorWithRed:currentColor.R green:currentColor.G blue:currentColor.B alpha:1.0];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

源码连接地址:http://pan.baidu.com/s/1jHrn2lw

转载于:https://www.cnblogs.com/DLS520/p/5182157.html

在 Android 应用中实现取色器,通常需要使用色盘和条形两种方式来选择颜色。其中,色盘用于选择颜色的明度和饱和度,条形用于选择颜色的色调。为了方便用户选择颜色,可以在色盘和条形上添加网格背景。 以下是实现方法: 1. 色盘网格背景 要在色盘上添加网格背景,可以使用 GradientDrawable 类创建一个圆形渐变背景,然后将该背景设置为色盘的背景。具体步骤如下: - 创建一个 drawable 资源文件(例如,grid_bg.xml),添加以下代码: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:type="radial" android:gradientRadius="50%p" android:centerX="50%" android:centerY="50%" android:startColor="#ffffffff" android:endColor="#00ffffff" android:angle="0" /> <size android:width="200dp" android:height="200dp"/> </shape> ``` - 在布局文件中,将该 drawable 资源文件设置为色盘的背景: ```xml <ImageView android:id="@+id/color_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/grid_bg"/> ``` 2. 条形网格背景 要在条形上添加网格背景,可以使用 GradientDrawable 类创建一个矩形渐变背景,然后将该背景设置为条形的背景。具体步骤如下: - 创建一个 drawable 资源文件(例如,grid_bg.xml),添加以下代码: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:type="linear" android:startColor="#ffffffff" android:endColor="#00ffffff" android:angle="0" /> <size android:width="200dp" android:height="30dp"/> </shape> ``` - 在布局文件中,将该 drawable 资源文件设置为条形的背景: ```xml <ImageView android:id="@+id/color_picker_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/grid_bg"/> ``` 注意:在上面的示例中,色盘和条形的大小都是 200dp x 200dp 和 200dp x 30dp,你可以根据自己的需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值