Skip to main content
technicaldaniel
Participant
April 10, 2016
Answered

How to get DefaultFillColor and DefaultStrokeColor?

  • April 10, 2016
  • 2 replies
  • 1032 views

Hi all,

I'm trying to fetch the DefaultFillColor and DefaultStrokeColor values back into my code for Adobe Illustrator. I can set these two values just fine by creating a new RGBColor() object and assigning to it, the reverse is proving a lot more difficult.

I am using C# as my coding language but I should be able to convert any other codes like JavaScript or VBScript back to C# if you don't know the language, so any help whatsoever or pointers would be helpful.

The code attempts I'm working with is along the lines of:

using ai = Illustrator;

ai.ApplicationClass app = new ai.ApplicationClass();

var ColObj = app.ActiveDocument.DefaultFillColor;

Console.WriteLine(ColObj);

//Returns System.__ComObject

var ColObj1 = (ai.RGBColor)app.ActiveDocument.DefaultFillColor;

Console.WriteLine(ColObj1.Red);

//Causes exception: Unable to cast COM object of type 'System.__ComObject' to interface type 'Illustrator.RGBColor'. No such interface supported.

Any advice on how to fetch these values in an RGB format, or even better - the Hex color code would be very much appreciated.

This topic has been closed for replies.
Correct answer technicaldaniel

Okay so I figured it out. What I believe is happening is the COM interface for Adobe Illustrator changes its properties/methods/attributes dynamically based on whether the Document mode of the ActiveDocument is set to RGB or CMYK.

Now because of this, directly querying the object returns a generic object instead, the attributes are actually still there but not accessible by just declaring a variable. Therefore, I needed to declare a dynamic instead.

dynamic ColObj = app.ActiveDocument.DefaultFillColor;

//if i know document is CMYK

var cyan = d.Cyan;

var magenta = d.Magenta;

//etc.

//if i know document is RGB

var red = d.Red;

var green = d.Green;

//etc.

Of course if I don't actually know the document mode and I try and poll the wrong type, it will throw an exception. Therefore I can use a loop such as this:

string colmode;

foreach (PropertyDescriptor descrip in TypeDescriptor.GetProperties(setCol))

{

     if (descrip.Name == "Cyan")

     {

           colmode = "CMYK";

           break;

     }

     else if (descrip.Name == "Red")

     {

           colmode = "RGB";

           break;

     }

}

to work it out by polling the method name of the first item, which will either be Cyan or Red.

C# is weird man..

2 replies

technicaldaniel
technicaldanielAuthorCorrect answer
Participant
April 11, 2016

Okay so I figured it out. What I believe is happening is the COM interface for Adobe Illustrator changes its properties/methods/attributes dynamically based on whether the Document mode of the ActiveDocument is set to RGB or CMYK.

Now because of this, directly querying the object returns a generic object instead, the attributes are actually still there but not accessible by just declaring a variable. Therefore, I needed to declare a dynamic instead.

dynamic ColObj = app.ActiveDocument.DefaultFillColor;

//if i know document is CMYK

var cyan = d.Cyan;

var magenta = d.Magenta;

//etc.

//if i know document is RGB

var red = d.Red;

var green = d.Green;

//etc.

Of course if I don't actually know the document mode and I try and poll the wrong type, it will throw an exception. Therefore I can use a loop such as this:

string colmode;

foreach (PropertyDescriptor descrip in TypeDescriptor.GetProperties(setCol))

{

     if (descrip.Name == "Cyan")

     {

           colmode = "CMYK";

           break;

     }

     else if (descrip.Name == "Red")

     {

           colmode = "RGB";

           break;

     }

}

to work it out by polling the method name of the first item, which will either be Cyan or Red.

C# is weird man..

CarlosCanto
Community Expert
Community Expert
April 11, 2016

great, thanks for sharing your findings.

Silly-V
Legend
April 10, 2016

How do you debug this C# code with Illustrator?

technicaldaniel
Participant
April 10, 2016

It's a C# windows form application which connects with Adobe Illustrator using its COM interface (Adobe Illustrator Type Inferface). The application attaches to Visual Studio's debugger.

CarlosCanto
Community Expert
Community Expert
April 10, 2016

which snippet is not working? the first one seems ok, not knowing the language this seems to be ok //Returns System.__ComObject

in Javascript we get either [CMYKColor] or [RGBColor] with this line

alert(app.activeDocument.defaultFillColor);

does this work?

  1. var ColObj = app.ActiveDocument.DefaultFillColor; 
  2. Console.WriteLine(ColObj.Red);