[M1 Mac, Big Sur 11.6.7, clang 13.0.0, NO IDE]
カラーコードを変換する動的ライブラリを作成しました。
コンパイラclang++のオプションとして-std=c++17が必要なのに気がつかず、少し手間取りました。何も付けないとC++03以前のレガシーC++としてコンパイルされるようです。
#pragma once
#include <string>
#include <vector>
#include <stdio.h>
#include <ctype.h>
#include <memory>
#include <sstream>
#include <iostream>
#include <unistd.h>
#include <iomanip>
using std::string; using std::vector;
using std::cout; using std::endl;
using std::to_string; using std::stoi;
class ColorConvert{
public:
vector<string> split(string str, char del);
vector<int> ZeroToRGB(string);
vector<int> SharpToRGB(string);
vector<int> HexToRGB(string);
string ZeroToSharp(string);
string ZeroToHex(string);
string SharpToZero(string);
string SharpToHex(string);
string HexToZero(string);
string HexToSharp(string);
string RGBToHex(string);
string RGBToString(vector<int>);
};
#include "ColorConvert.h"
vector<string> ColorConvert::split(string str, char del) {
int first = 0;
int last = str.find_first_of(del);
vector<string> result;
while (first < str.size()) {
string subStr(str, first, last - first);
result.push_back(subStr);
first = last + 1;
last = str.find_first_of(del, first);
if (last == string::npos) {
last = str.size();
}
}
return result;
}
vector<int> ColorConvert::ZeroToRGB(string color_zero){
vector<int> RGBColor;
string red0 = color_zero.substr(2,2);
int red = stoi(red0, nullptr, 16);
RGBColor.push_back(red);
string green0 = color_zero.substr(4,2);
int green = stoi(green0, nullptr, 16);
RGBColor.push_back(green);
string blue0 = color_zero.substr(6,2);
int blue = stoi(blue0, nullptr, 16);
RGBColor.push_back(blue);
return RGBColor;
}
vector<int> ColorConvert::SharpToRGB(string color_sharp){
vector<int> RGBColor;
string red0 = color_sharp.substr(1,2);
int red = stoi(red0, nullptr, 16);
RGBColor.push_back(red);
string green0 = color_sharp.substr(3,2);
int green = stoi(green0, nullptr, 16);
RGBColor.push_back(green);
string blue0 = color_sharp.substr(5,2);
int blue = stoi(blue0, nullptr, 16);
RGBColor.push_back(blue);
return RGBColor;
}
vector<int> ColorConvert::HexToRGB(string color_hex){
vector<int> RGBColor;
string red0 = color_hex.substr(0,2);
int red = stoi(red0, nullptr, 16);
RGBColor.push_back(red);
string green0 = color_hex.substr(2,2);
int green = stoi(green0, nullptr, 16);
RGBColor.push_back(green);
string blue0 = color_hex.substr(4,2);
int blue = stoi(blue0, nullptr, 16);
RGBColor.push_back(blue);
return RGBColor;
}
string ColorConvert::ZeroToSharp(string color_zero){
string color_hex = color_zero.erase(0,2);
cout << "color_hex " << color_hex.c_str() << endl;
string color_sharp = "#" + color_hex;
return color_sharp;
}
string ColorConvert::ZeroToHex(string color_zero){
string color_hex = color_zero.erase(0,2);
cout << "color_hex " << color_hex.c_str() << endl;
return color_hex;
}
string ColorConvert::SharpToZero(string color_sharp){
string color_hex = color_sharp.erase(0,1);
cout << "color_hex " << color_hex.c_str() << endl;
string color_zero = "0x" + color_hex;
return color_zero;
}
string ColorConvert::SharpToHex(string color_sharp){
string color_hex = color_sharp.erase(0,1);
cout << "color_hex " << color_hex.c_str() << endl;
return color_hex;
}
string ColorConvert::HexToZero(string color_hex){
string color_zero = "0x" + color_hex;
return color_zero;
}
string ColorConvert::HexToSharp(string color_hex){
string color_sharp = "#" + color_hex;
return color_sharp;
}
string ColorConvert::RGBToHex(string color_rgb){
vector<string> rgb_list = ColorConvert::split(color_rgb, ',');
vector<string> red00 = ColorConvert::split(rgb_list[0], '(');
vector<string> blue00 = ColorConvert::split(rgb_list[2], ')');
string red0 = red00[1];
string green0 = rgb_list[1];
string blue0 = blue00[0];
cout << "red0 " << red0.c_str() << " green0 " << green0.c_str() << " blue0 " << blue0.c_str() <<endl;
int red = stoi(red0);
int green = stoi(green0);
int blue = stoi(blue0);
// 10進数から16進数へ変換
std::stringstream ss1;
ss1 << std::hex << std::setw(2) << std::setfill('0') << red;
string red_hex = ss1.str();
cout << red_hex.c_str() << endl;
std::stringstream ss2;
ss2 << std::hex << std::setw(2) << std::setfill('0') << green;
string green_hex = ss2.str();
cout << green_hex.c_str() << endl;
std::stringstream ss3;
ss3 << std::hex << std::setw(2) << std::setfill('0') << blue;
string blue_hex = ss3.str();
cout << blue_hex.c_str() << endl;
string color_hex = red_hex + green_hex + blue_hex;
return color_hex;
}
string ColorConvert::RGBToString(vector<int> RGBColor){
int red = RGBColor[0];
int green = RGBColor[1];
int blue = RGBColor[2];
string color_rgb = "RGB(" + to_string(red) + "," + to_string(green) + "," + to_string(blue) + ")";
return color_rgb;
}
clang++ -dynamiclib -o ColorConvert.dylib \
ColorConvert.cpp \
-I/code/cpp/mylib/include -std=c++17