iOS开发中十六进制颜色与RGB的相互转化

在iOS开发中,我们常常会在界面色调搭配的时候选色比较麻烦,因为系统大多提供给我们的是一些基本颜色,还有就是取色器强行配色。但实际我们所需要的更多色调都有具体的十六进制表示,接下来我们就看看十六进制颜色与我们经常使用的RGB值之间的转换

1.由于文中经常用到,所以我直接在一个类中封装一个HEX转化方法,以便后面方便使用:

.h文件:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface HexColor : NSObject

/*.h中进行申明方法*/

+ (UIColor *)colorWithHexString:(NSString *)color;

@end

.m文件:

#import "HexColor.h"
@implementation HexColor
#pragma mark -- 16进制Color转换

+ (UIColor *)colorWithHexString:(NSString *)color

{
    NSString *cString = [[colorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]uppercaseString];

    if ([cStringlength] < 6) {
        return [UIColorclearColor];
    }

    if ([cStringhasPrefix:@"0X"])

        cString = [cString substringFromIndex:2];

    if ([cStringhasPrefix:@"#"])

        cString = [cString substringFromIndex:1];

    if ([cStringlength] != 6)

        return [UIColorclearColor];

    NSRange range;
    range.location =0;
    range.length =2;
    NSString *rString = [cStringsubstringWithRange:range];
    range.location =2;
    NSString *gString = [cStringsubstringWithRange:range];
    range.location =4;
    NSString *bString = [cStringsubstringWithRange:range];
    unsignedint r, g, b;

    [[NSScannerscannerWithString:rString] scanHexInt:&r];
    [[NSScannerscannerWithString:gString] scanHexInt:&g];
    [[NSScannerscannerWithString:bString] scanHexInt:&b];

    return [UIColorcolorWithRed:((float) r /255.0f) green:((float) g /255.0f) blue:((float) b /255.0f) alpha:1.0f];
}
@end

2.例如在某个颜色设置中调用:

 self.view.backgroundColor = [HexColorcolorWithHexString:@"a1f1f2"];

PS:这样就可以方便我们随意搭配了。

以下是一个多线程的二进制文件和十六进制文件相互转化的示例代码,你可以在 VS2013 进行编译和运行: ```c++ #include <iostream> #include <fstream> #include <string> #include <thread> #include <mutex> using namespace std; // 互斥锁,用于保护文件读写操作 mutex mtx; // 将二进制文件转化十六进制字符串 void bin2hex(string bin_file_path, string hex_file_path) { ifstream bin_file(bin_file_path, ios::binary); ofstream hex_file(hex_file_path); if (!bin_file || !hex_file) { cout << "文件打开失败!" << endl; return; } // 获取文件大小 bin_file.seekg(0, ios::end); int file_size = bin_file.tellg(); bin_file.seekg(0, ios::beg); // 每次读取 1KB 数据进行转化 const int BUF_SIZE = 1024; char buf[BUF_SIZE]; for (int i = 0; i < file_size; i += BUF_SIZE) { int len = min(file_size - i, BUF_SIZE); bin_file.read(buf, len); // 将每个字节转化为两个十六进制字符 for (int j = 0; j < len; j++) { char hex_str[3]; sprintf_s(hex_str, "%02X", (unsigned char)buf[j]); mtx.lock(); hex_file << hex_str; mtx.unlock(); } } bin_file.close(); hex_file.close(); } // 将十六进制字符串转化为二进制文件 void hex2bin(string hex_file_path, string bin_file_path) { ifstream hex_file(hex_file_path); ofstream bin_file(bin_file_path, ios::binary); if (!hex_file || !bin_file) { cout << "文件打开失败!" << endl; return; } // 每次读取 1KB 数据进行转化 const int BUF_SIZE = 1024; char buf[BUF_SIZE]; while (hex_file.get(buf, BUF_SIZE + 1)) { int len = hex_file.gcount(); // 将每两个十六进制字符转化为一个字节 for (int i = 0; i < len; i += 2) { unsigned char byte = 0; for (int j = 0; j < 2; j++) { byte <<= 4; if (buf[i + j] >= '0' && buf[i + j] <= '9') { byte |= buf[i + j] - '0'; } else if (buf[i + j] >= 'A' && buf[i + j] <= 'F') { byte |= buf[i + j] - 'A' + 10; } else if (buf[i + j] >= 'a' && buf[i + j] <= 'f') { byte |= buf[i + j] - 'a' + 10; } else { cout << "错误的十六进制字符:" << buf[i + j] << endl; return; } } mtx.lock(); bin_file.put(byte); mtx.unlock(); } } hex_file.close(); bin_file.close(); } int main() { // 测试二进制文件转化十六进制字符串 string bin_file_path = "test.bin"; string hex_file_path = "test.hex"; thread t1(bin2hex, bin_file_path, hex_file_path); // 测试十六进制字符串转化为二进制文件 string hex_file_path2 = "test2.hex"; string bin_file_path2 = "test2.bin"; thread t2(hex2bin, hex_file_path2, bin_file_path2); // 等待两个线程执行完成 t1.join(); t2.join(); return 0; } ``` 代码使用了两个 `thread` 对象,分别表示将二进制文件转化十六进制字符串和将十六进制字符串转化为二进制文件的操作。在每个线程,使用了 `ifstream` 和 `ofstream` 对象来读取和写入文件,并使用了 `mutex` 对象来保护文件读写操作,以避免多个线程同时访问同一个文件导致的数据竞争问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值