How to retrieve HSB from CGColor? - iphone

I have a CGColor in my app and I'd like to get the hue, saturation and brightness values from it. How would i be able to access these?

See Duncan Champney's post to cocoa-dev entitled "Here is code to Convert RGB <-> HSB". There, he gives this code:
//------------------
//---header file
#define RETURN_HSV(h, s, v) {HSV.H = h; HSV.S = s; HSV.V = v; return
HSV;}
#define RETURN_RGB(r, g, b) {RGB.R = r; RGB.G = g; RGB.B = b; return
RGB;}
#define UNDEFINED 0
// Theoretically, hue 0 (pure red) is identical to hue 6 in these
transforms. Pure
// red always maps to 6 in this implementation. Therefore UNDEFINED
can be
// defined as 0 in situations where only unsigned numbers are desired.
typedef struct {float R, G, B;} RGBType;
typedef struct {float H, S, V;} HSVType;
//------------------
//--The code
#include <math.h>
HSVType RGB_to_HSV( RGBType RGB )
{
// RGB are each on [0, 1]. S and V are returned on [0, 1] and H is
// returned on [0, 1]. Exception: H is returned UNDEFINED if S==0.
float R = RGB.R, G = RGB.G, B = RGB.B, v, x, f;
int i;
HSVType HSV;
//x = fminx(R, G, B);
x = fminf(R, G);
x = fminf(x, B);
//v = fmaxf(R, G, B);
v = fmaxf(R, G);
v = fmaxf(v, B);
if(v == x) RETURN_HSV(UNDEFINED, 0, v);
f = (R == x) ? G - B : ((G == x) ? B - R : R - G);
i = (R == x) ? 3 : ((G == x) ? 5 : 1);
RETURN_HSV(((i - f /(v - x))/6), (v - x)/v, v);
}
RGBType HSV_to_RGB( HSVType HSV )
{
// H is given on [0, 1] or UNDEFINED. S and V are given on [0, 1].
// RGB are each returned on [0, 1].
float h = HSV.H * 6, s = HSV.S, v = HSV.V, m, n, f;
int i;
RGBType RGB;
if (h == 0) h=.01;
if(h == UNDEFINED) RETURN_RGB(v, v, v);
i = floorf(h);
f = h - i;
if(!(i & 1)) f = 1 - f; // if i is even
m = v * (1 - s);
n = v * (1 - s * f);
switch (i)
{
case 6:
case 0: RETURN_RGB(v, n, m);
case 1: RETURN_RGB(n, v, m);
case 2: RETURN_RGB(m, v, n);
case 3: RETURN_RGB(m, n, v);
case 4: RETURN_RGB(n, m, v);
case 5: RETURN_RGB(v, m, n);
}
RETURN_RGB(0, 0, 0);
}
//------------------

Related

How to convert HSV to RGB in flutter

I'm using this code to convert RGB to HSV.
now i have to revert back to RGB.
is that any way to convert this in flutter??
I just recently started working in flutter, please help me out, I'm stuck here.
Thank you so much in advance.
rgbToHsv(int r1, int g1, int b1) {
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1:
double r = r1 / 255.0;
double g = g1 / 255.0;
double b = b1 / 255.0;
// h, s, v = hue, saturation, value
var cmax = [r, g, b].reduce(max); // maximum of r, g, b
var cmin = [r, g, b].reduce(min); // minimum of r, g, b
var diff = cmax - cmin; // diff of cmax and cmin.
var h;
var s;
var v;
//Get value of h
if (cmax == cmin) {
h = 0;
} else if (cmax == r) {
h = (60 * ((g - b) / diff) + 360) % 360;
} else if (cmax == g) {
h = (60 * ((b - r) / diff) + 120) % 360;
} else if (cmax == b) {
h = (60 * ((r - g) / diff) + 240) % 360;
}
//Get value of s
if (cmax == 0) {
s = 0;
} else {
s = (diff / cmax) * 100;
}
//Get value of v
v = cmax * 100;
//Convert HSV [360, 100, 100] to HSV [256, 256, 256]
double h_256 = (h / 360) * 255;
double s_256 = (s / 100) * 255;
double v_256 = (v / 100) * 255;
int h_256_int = h_256.toInt();
int s_256_int = s_256.toInt();
int v_256_int = v_256.toInt();
//Convert to HSV HEX
var hex_h = h_256_int.toRadixString(16);
var hex_s = s_256_int.toRadixString(16);
var hex_v = v_256_int.toRadixString(16);
//MERGE HSV HEX
var finalColor = hex_h + hex_s + hex_v;
print("HSV HEX:" + finalColor.toUpperCase());
/////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
////////RGB TRANS>>>>>>>>>>>>>>>>>
var _h = hextToint(hex_h);
var _s = hextToint(hex_s);
var _v = hextToint(hex_v);
print(_h);
print(_s);
print(_v);
print(hsvToRgb(_h.toDouble(), _s.toDouble(), _v.toDouble()));
//return rgb;
}
this might help. I have used this in one of my projects.
String hsvToRgb(double H, double S, double V) {
int R, G, B;
H /= 360;
S /= 100;
V /= 100;
if (S == 0) {
R = (V * 255).toInt();
G = (V * 255).toInt();
B = (V * 255).toInt();
} else {
double var_h = H * 6;
if (var_h == 6) var_h = 0; // H must be < 1
int var_i = var_h.floor(); // Or ... var_i =
// floor( var_h )
double var_1 = V * (1 - S);
double var_2 = V (1 - S (var_h - var_i));
double var_3 = V (1 - S (1 - (var_h - var_i)));
double var_r;
double var_g;
double var_b;
if (var_i == 0) {
var_r = V;
var_g = var_3;
var_b = var_1;
} else if (var_i == 1) {
var_r = var_2;
var_g = V;
var_b = var_1;
} else if (var_i == 2) {
var_r = var_1;
var_g = V;
var_b = var_3;
} else if (var_i == 3) {
var_r = var_1;
var_g = var_2;
var_b = V;
} else if (var_i == 4) {
var_r = var_3;
var_g = var_1;
var_b = V;
} else {
var_r = V;
var_g = var_1;
var_b = var_2;
}
R = (var_r * 255).toInt(); // RGB results from 0 to 255
G = (var_g * 255).toInt();
B = (var_b * 255).toInt();
}
String rs = R.toRadixString(16);
String gs = G.toRadixString(16);
String bs = B.toRadixString(16);
if (rs.length == 1) rs = "0" + rs;
if (gs.length == 1) gs = "0" + gs;
if (bs.length == 1) bs = "0" + bs;
return "#" + rs + gs + bs;
}
RGB to HSV
HSVColor rgbToHSV(int r, int g, int b, {double opacity = 1}) {
return HSVColor.fromColor(Color.fromRGBO(r, g, b, opacity));
}
HSV to RGB
List<int> hsvToRGB(HSVColor color) {
//convert to color
final c = color.toColor();
return [c.red, c.blue, c.green];
}
To use HSVColor use myHSVcolor.toColor().
More about HSV Color.
Expanding on Yeasin Sheikh answer
Flutter
RGB to HSV
HSVColor rgbToHSV(int r, int g, int b, {double opacity = 1}) {
return HSVColor.fromColor(Color.fromRGBO(r, g, b, opacity));
}
HSV to RGB
List<int> hsvToRGB(HSVColor color) {
//convert to color
final c = color.toColor();
return [c.red, c.blue, c.green];
}
To use HSVColor use myHSVcolor.toColor().
More about HSV Color.
Native Dart
Import color package
RGB to HSV
final RgbColor rgbColor = RgbColor(red, green, blue);
final HsvColor hsvColor = rgbColor.toHsvColor();
HSV to RGB
final HsvColor hsvColor = HsvColor(hueValue, saturationValue, valueValue)
final RgbColor rgbColor = hsvColor.toRgbColor();
HSVColor class is not supported in native dart so we are using a package for that with similar classes.

How can I get a proper double form for a symbolic matrix?

How can I get a proper double form from this symbolic Matrix in MatLab? I've tried everything but I prefer not using feval or inline function as they're not recommended
This is the code to get the matrix
function T = Romberg (a, b, m, f)
T = zeros(m, m);
T = sym(T);
syms f(x) c h;
f(x) = f;
c = (subs(f,a)+subs(f,b)) / 2;
h = b - a;
T(1,1) = h * c;
som = 0 ;
n = 2;
for i = 2 : m
h = h / 2;
for r = 1 : n/2
som = som + subs(f,(a + 2*(r-1)*h));
T(i,1) = h * (c + som);
n = 2*n;
end
end
r = 1;
for j = 2 : m
r = 4*r;
for i = j : m
T(i,j) = (r * T(i, j-1) - T(i-1,j-1)/(r-1));
end
end
end
And with an input like this
Romberg(0, 1, 4, '2*x')
I get a symbolic matrix output with all the
3 * f(3)/2 + f(1)/2 + f(5)/2
I would like to have a double output.
Can you please help me?
Thank you very much in advance!

Plotting iteratively defined function in MATLAB

I'm not a MATLAB professional and so, I need some help at producing a 3D plot of the iteratively defined function f : R^2-0 -> R defined below (in pseudocode) for x,y values in [-1,1] and
For each I = 1,2,3 and A = (0.5,0.5)
function f(v in R^2-0)
{
a=b=0; (a,b in R)
for (i=0; i<I; i=i+1)
{
v = |v| / ||v||^2 - A;
a = a + | ||v||-b |;
b = ||v||;
}
return a;
}
(|v| denotes component-wise vector absolute value)
(If you want you can look at the fractal that is generated by the function at my question on math-exchange here:
https://math.stackexchange.com/questions/1457733/a-question-about-a-fractal-like-iteratively-defined-function
)
MATLAB code to do that will be appreciated.
Much Thanks.
Save this your main program:
clear
clc
close all
% I = 1;
% A = [ 0.5 0.5 ];
I = 10;
A = [ 0.5 0.5 0.5 ];
xmin = -1;
xmax = 1;
ymin = -1;
ymax = 1;
nx = 101;
ny = 101;
dx = (xmax - xmin) / (nx - 1);
dy = (ymax - ymin) / (ny - 1);
x = xmin: dx: xmax;
y = ymin: dy: ymax;
for ix = 1: nx
for iy = 1: ny
if (length(A) == 2)
z(iy, ix) = f([x(ix) y(iy)], A, I);
elseif (length(A) == 3)
z(iy, ix) = f([x(ix) y(iy) 0], A, I);
end
end
end
pcolor(x, y, z)
shading interp
Then save this function in the same directory as the main program as f.m:
function result = f(v, A, I)
a = 0;
b = 0;
for i = 1: I
v = abs(v) / dot(v, v) - A;
a = a + abs(norm(v) - b);
b = norm(v);
end
result = a;
end

Pentadiagonal Matrix in OpenCV

I'm trying to implement the active contour models algorithm with c++ and opencv in VisualStudio.
I'm having trouble creating a pentadiagonal matrix, that is a matrix with the five main diagonals with values and zeros in the rest of the matrix.
I've already implemented this in MatLab, that came out like this:
K = diag(repmat(a,1,n));
K = K + diag(repmat(b,1,n-1),1) + diag(repmat(b,1,n-1),-1)...
+ diag(b,n-1) + diag(b,-n+1);
K = K + diag(repmat(c,1,n-2),2) + diag(repmat(c,1,n-2),-2)...
+ diag([c c],n-2) + diag([c c],-n+2);
How do I do the same thing in opencv?
Thanks!
You can wrap the Matlab functions repmat and diag as in the code below.
Then you can write the OpenCV code as in Matlab. The only difference is that you cannot concatenate 2 matrices with [c c], but you should use repmat(c, 2, 1), or repmat(c,1,2) for [c; c].
Code:
#include <opencv2\opencv.hpp>
using namespace cv;
Mat repmat(const Mat& src, int nx, int ny)
{
return repeat(src, nx, ny);
}
Mat diag(const Mat& src, int k=0)
{
// src must be a row or column matrix
//CV_Assert(src.rows == 1 || src.cols == 2);
int n = src.rows * src.cols;
Mat1d _src;
src.convertTo(_src, CV_64F);
// Create output matrix of correct dimension
int dim = n + abs(k);
Mat1d _dst(dim, dim, double(0.0));
// Select the ranges where to put src data
Range rrows;
Range rcols;
if (k >= 0)
{
rrows = Range(0, n);
rcols = Range(k, k+n);
}
else
{
rrows = Range(-k, -k + n);
rcols = Range(0, n);
}
// Create square n x n submatrix
Mat1d sub(_dst(rrows, rcols));
// Put data on the diagonal of the submatrix
for (int i = 0; i < n; ++i)
{
sub(i, i) = _src(i);
}
Mat dst;
_dst.convertTo(dst, src.type());
return dst;
}
int main()
{
Mat a;
Mat b;
Mat c;
int n;
// ... init a, b, c, n
Mat K;
K = diag(repmat(a, 1, n));
K = K + diag(repmat(b, 1, n - 1), 1) + diag(repmat(b, 1, n - 1), -1) + diag(b, n - 1) + diag(b, -n + 1);
K = K + diag(repmat(c, 1, n - 2), 2) + diag(repmat(c, 1, n - 2), -2) + diag(repmat(b, 2, 1), n - 2) + diag(repmat(b, 2, 1), -n + 2);
return 0;
}

compile error in shader ourside of code

I have the following attempt at translating the perlin noise to the GPU in
unity compute shader:
#pragma kernel CSMain
RWTexture2D<float4> Result;
[numthreads(8,8,1)]
//based on http://mrl.nyu.edu/~perlin/noise/
void CSMain (uint3 id : SV_DispatchThreadID)
{ float res = noise((float)id.x,(float)id.y,0.0f);
Result[id.xy] = float4(res,res,res,res);
}
int p[256]= {151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
double lerp(double t, double a, double b) { return a + t * (b - a); }
double grad(int hash, double x, double y, double z) {
int h = hash & 15;
double u = h<8 ? x : y,
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
double noise(float xx, float yy, float zz){
int X = (int)floor(xx) & 255;
int Y = (int)floor(yy) & 255;
int Z = (int)floor(zz) & 255;
int x = -floor(x);
int y = -floor(y);
int z = -floor(z);
double u = fade(x);
double v = fade(y);
double w = fade(z);
}
However this gives me an error on line 57 (the code is 47 lines long)
Shader error in "PerlinClouds.ompute": noise(floatM|halfM|min10floatM|min16floatM) at line 57 (on)
Does anybody know what that means? It's not even in my code so I don't quite know where to look.
Make sure you define functions earlier in the file than you first try and use them.
In your noise function:
You don't return a value.
You initialise 'x' on the same line as you use it. You might have meant:
int x = -floor(X);
But maybe you meant:
int x = -floor(xx);
Don't use different case of the same variables (x, xx and X); you appear to have confused yourself and probably anyone reading your code.