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.
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;
}
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.