Forum

Color conversion in c# plugin

Hello,

I’m trying to implement a color picker in a plugin but I can’t find how to output the selected color.

Error is :
Cannot implicitly convert System.Window.Media.Color to VVVV.Utils.VColor.RGBAColor

I found few lines in plugin references about color conversion and I thought it was straight forward :

“There is an implicit cast to the C# Color type and an explictit cast from C# color to RGBAColor.”

Here is the few line which ended with an error on the last line :

var uiElement = (ColorPicker)UIElementOut[i](i);
System.Windows.Media.Color col = new System.Windows.Media.Color();
col = (System.Windows.Media.Color)uiElement.SelectedColor;
ValueOut[i](i) = col;

ValueOut is RGBAColor output pin.

What about
ValueOuti = new RGBAColor(col);

ah, there is this subtle difference:
System.Drawing.Color vs. System.Windows.Media.Color

here is how to convert from the media one (which wpf is using) to the drawing one (which RGBAColor can handle):

Thanks it’s working now.

But one more question ;) if no color are selected in the colorpicker apparently the default outputted color is black. Is there a way to output NIL from output pin by default ?

when I try : ValueOuti = null; Visual Studio tells me that RGBAColor is non nullable.

Cheers

use this instead to indicate an empty spread:

ValueOut.SliceCount = 0;

Thanks guys, got it nice and smooth now. More to come ;)

one more question ! what would be the other way around ?

RGBAColor to C#

thx

you should already understand that this is not a precise question since .net obviously comes with two different color types. anyway both of those have a .FromARGB() function which you can use with the individual components of the RGBAColor. see?

Sorry but I don’t “see” really clearly probably because I’m beginning with c# :p

Actually those two lines are giving me the same error :

System.Drawing.Color col = System.Drawing.Color.FromArgb(FBgCol.A, FBgCol.R, FBgCol.G, FBgCol.B);
System.Windows.Media.Color col = System.Windows.Media.Color.FromArgb(FBgCol.A, FBgCol.R, FBgCol.G, FBgCol.B);

‘ISpread’ does not contain a definition for ‘A’ and no extension method ‘A’ accepting a first argument of type ‘ISpread’ could be found (are you missing a using directive or an assembly reference?)

While my RGBAColor comes from this input :

[Input("Background Color", DefaultColor = new double[](Input("Background Color", DefaultColor = new double[) { 0.1, 0.2, 0.3, 1.0 })]
        public ISpread<RGBAColor> FBgCol;

you’re not accessing the individual slices of your field, more like:

System.Drawing.Color col = System.Drawing.Color.FromArgb(FBgCol[i](i).A, FBgCol[i](i).R, FBgCol[i](i).G, FBgCol[i](i).B);

or

RGBAColor c = FBgCol[i](i);
System.Drawing.Color col = System.Drawing.Color.FromArgb(c.A, c.R, c.G,c.B);

and wrapped in a for loop …

Thanks, I was trying something similar but it says : cannot convert double to int.

System.Drawing is from 0 to 255 and RGBAColor is from 0 to 1 as far as I understand so I guess i have to do the conversion manually in code ?

that would be

System.Drawing.Color.FromArgb((int)c.A*255, (int)c.R*255, (int)c.G*255, (int)c.B*255);

maybe there’s some shorter way?

Here is how I made it :

Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i](i)));
            Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i](i)));
            uiElement.Background = BgBrushColor;
            uiElement.Foreground = FgBrushColor;
        }
        private Color RGBAToMediaColor(RGBAColor c)
        {
            System.Drawing.Color col = System.Drawing.Color.FromArgb(Convert.ToInt32(c.A * 255), Convert.ToInt32(c.R * 255), Convert.ToInt32(c.G * 255), Convert.ToInt32(c.B * 255));
            System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(col.A, col.R, col.G, col.B);
            return color;
        }

I thought there were an nicer way to do it, but it works now.

intead of

csharp:
private Color RGBAToMediaColor(RGBAColor c)
        {
            System.Drawing.Color col = System.Drawing.Color.FromArgb(Convert.ToInt32(c.A * 255), Convert.ToInt32(c.R * 255), Convert.ToInt32(c.G * 255), Convert.ToInt32(c.B * 255));
            System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(col.A, col.R, col.G, col.B);
            return color;
        }

you can go directly

csharp:
private Color RGBAToMediaColor(RGBAColor c)
        {
           return System.Windows.Media.Color.FromArgb[int)(c.A * 255), (int)(c.R * 255), (int)(c.G * 255), (int)(c.B * 255](https://vvvv.org/documentation/int)(c.A-*-255),-(int)(c.R-*-255),-(int)(c.G-*-255),-(int)(c.B-*-255);
        }

@tonfilm

in this case VS says it can’t convert int to byte.

ah, then (byte) instead of (int)…