色盘取出点击处的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

时间: 2024-09-30 18:30:17

色盘取出点击处的R,G,B色值(的相关文章

python Image PNG getpixel R/G/B/A

# python Image PNG getpixel R/G/B/A# # 说明: # 本文主要是记录python中如何使用Image模块进行基本的图像R.G.B.A值得获取. # 为后续的raspberry pi进行图像处理做验证. # # 2016-2-10 抚州 崇仁马鞍港上村 曾剑锋 import Image # 加载png图片 img = Image.open("vim-cmd.png") # 输出图像的基本信息 print img.format, img.size, im

javascript把RGB指定颜色转换成十六进制颜色(Converting R,G,B values to HTML hex notation)

Prologue 看见一篇很好的外国文章,Making annoying rainbows in javascript,其实我当时很想把它翻译下来的,但是对于一个连六级都没过的人确实有点难度,一些文段看起来是知道意思但是就不知道怎么用自己的话来表达.越来越觉得对于IT行业来说英语至少要达到能读的程度,毕竟好的东西很多是外国的,还有哦,要会翻墙.对于那篇文章我其实还没看完,还在慢慢消化中,但是看到一个函数确实真的很吸引我,实现的是Converting R,G,B values to HTML he

matlab实现将彩色图像(R,G,B)色分量的直方图匹配,并计算其相关性

函数 实现将彩色图像转化为直方图分量向量 calcrgb2hist.m 文件 function hist  = calcrgb2hist(picname) pic1 = imread(picname); pic1R = pic1(:,:,1); pic1G = pic1(:,:,2); pic1B = pic1(:,:,3); figure,imshow(pic1R)                  title('R分量的图像') % 二,绘制直方图 [m,n]=size(pic1R);  

颜色(color)转换为三色值(r/g/b)(干货分享)

//颜色转换  ##665522 - 三色值 + (UIColor *)setFontColorWithString:(NSString *)color { NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]] uppercaseString]; // 字符串个数必须大于等于6 if ([cString length] < 6) {

Android开发:setAlpha()方法和常用RGB颜色表----颜色, r g b分量数值(int), 16进制表示 一一对应

杂家前文Android颜色对应表只有颜色和十六进制,有时候需要设置r g b分量的int值,如paint.setARGB(255, 127, 255, 212);就需要自己计算下分量的各个值.这里提供一个带有r g b分量的int型的颜色表.注意paint.setAlpha()及paint.setARGB()的第一个参数,即透明度.其取值范围是0---255,数值越小,越透明,颜色上表现越淡.实际上当设成10以下就会有透明的效果了. 注意:这个透明度是用来设置画笔paint的透明度,然后用pai

Android ListView A-Z侧边栏字母排序,点击处扭曲成半圆

最近在做通讯录看到360通讯录和索尼系统内置通讯录有这个效果,就是点击联系人列表的A-Z侧边栏时,A-Z栏的被触摸处会扭曲成半圆,半圆可以随着手指在A-Z移动.索尼手机的比较高级,扭曲和恢复的过程有阻尼效果,360的没有任何效果就一伸一缩.所本人实现了个360效果一摸一样的,A-Z侧边栏.     一.实现原理讲解: 我们知道正常的A-Z栏每个字母的坐标的Y值应该是一样的(起码差不多一样),所以绘制出来的时候三竖直的.因此我只要按照如下步骤,就可以实现图中的效果: 1.当A-Z栏被触摸时,通过s

JavaScript基础 event(For IE) 显示鼠标点击处的坐标值 距离窗口的距离 距离网页的距离 距离屏幕的距离

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"

JavaScript基础 在鼠标点击处生出图片(鼠标在哪点,图片在哪显示)

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: <!DOCTYPE html> <html onclick="CreateImg(event)"> <head> <meta http-equiv="Content-Type" conten

Matlab 提取R,G,B颜色分量

>> im = imread('ny.png'); >> r = im(:,:,1); >> g = im(:,:,2); >> b = im(:,:,3); >> imshow(r), imshow(g),imshow(b); 分别显示有颜色的 >> r1 = cat(3, r, zeros(size(r)), zeros(size(r))); >> g1 = cat(3, zeros(size(g)), g, zero