Set FillColor for Character - adobe-illustrator

I have a little issue with FillColor attribute. From scripting reference it seems that it is possible to set it with RGB color.
RGBColor RGB = new RGBColor();
.CharacterAtributes.FillColor = RGB;
But I keep getting error if I try to invert it
RGB = .CharacterAtributes.FillColor;
Even if add reference to RGB color
RGB = .CharacterAtributes.FillColor.RGBColor;
From what I know, FillColor should be color object which contains object of RGBColor. But if I check on debug it contains only CMYK values as double. Is it known glitch, or I am missing something to get fill color as RGB?
Thank you,
Michał

My understanding of how color objects work is that FillColor will contain a color object that will have ONE of the following sub classes:
CMYKColor
GradientColor
GrayColor
LabColor
NoColor
PatternColor
RGBColor
SpotColor
It sounds like your fill color is set to a CMYKColor, if you need an RGBColor you will have to do a conversion from CMYK and create it with the converted values. There are no built in color conversion libraries in the scripting API that I am aware of, but there are several libraries available. Here is one I found quick Javascript color conversion RGB, CMYK and HSV
Good luck.

I see whre was error in my understanding of FillColor. It will be any of those classes, not have:
CMYKColor
GradientColor
GrayColor
LabColor
NoColor
PatternColor
RGBColor
SpotColor
In that case assigment should look like this:
RGB.Red = .CharacterAtributes.FillColor.Red;
RGB.Green = .CharacterAtributes.FillColor.Green;
RGB.Blue = .CharacterAtributes.FillColor.Blue;
Michael

Related

NSTextBlock backgroundColor is not drawn

I have an NSTextBlock subclass that has a specific backgroundColor set. Now when I add a custom paragraph style to a range of text like this
let block = MyTextBlock()
block.backgroundColor = myBackgroundColor
let pstyle = NSMutableParagraphStyle()
pstyle.textBlocks = [block]
attributedText.addAttribute(.paragraphStyle, value: pstyle, range: textRange)
// Append the attributed string to the text views textStorage ...
the text block is shown without a background color. I know that the text blocks works, because rectForLayout gets called, but when I try to override drawBackground it never gets called.
Do I have to do something else for NSTextBlocks to draw their background?
PS: Borders also seem to be ignored. I also tried to find a sample on Github, but that also doesn't draw any backgrounds, despite having a background color set.
After trying everything, I finally managed to get the background to show up, by setting
setValue(100, type: .percentageValueType, for: .width)
It seems that the drawing logic expects some value for the content size. Really nice documentation job there. This requirement is nowhere to be found.

CreateJs Drawing with alpha

I implemented a little drawing function into my app with CreateJS like so:
var currentPosition = this.posOnStage(event);
var drawing = container.getChildByName('drawing');
drawing.graphics.ss(this.brushSize, "round").s(this.brushColor);
drawing.graphics.mt(this._lastMousePosition.x, this._lastMousePosition.y);
drawing.graphics.lt(currentPosition.x, currentPosition.y);
drawing.alpha = this.brushAlpha;
container.updateCache(this.enableErasing ? "destination-out" : "source-over");
drawing.graphics.clear();
this._lastMousePosition = this.posOnStage(event);
As you can see, the alpha value of this drawing can change. Sadly you can draw over a point you once did draw, so when you draw over a point multiple times the alpha effect will go away. Any idea how to solve this ?
Thanks :)
EDIT:
I tried it like gskinner and Lanny 7 proposed, but it didn't work. I attached a image so you can see the problem.
As suggested by Lanny, apply the alpha to the actual stroke, not to the Shape. You can use Graphics methods to help with this.
For example:
// set the brush color to red with the current brush alpha:
this.brushColor = createjs.Graphics.getRGB(255, 0, 0, this.brushAlpha);

Objective-C - CTFont change font style?

I have a CTFont that contains a font style, and sumbolic traits.
I want to create a new font with a new style that inherits the symbolic traits of the first font.
How can I achieve this?
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFString)newFontName, CTFontGetSize(font), NULL);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, CTFontGetSize(font), NULL, 0, CTFontGetSymbolicTraits(font));
the new font is null here
I don't know what should I pass to the 4th parameter in CTFontCreateCopyWithSymbolicTraits.
I do this line of code to generate a bold font from non-bold font:
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(currentFont, 0.0, NULL, (wantBold?kCTFontBoldTrait:0), kCTFontBoldTrait);
currentFont is the CTFontRef I want to add symbolic traits to
wantBold is a boolean to tell if I want to add or remove the bold trait to the font
kCTFontBoldTrait is the symbolic trait I want to modify on the font.
The 4th parameter is the value you want to apply. The 5th is the mask to select the symbolic trait.
You may thing of it as bitmask to apply to the symbolic trait, where the 4th parameter of CTFontCreateCopyWithSymbolicTraits is the value and the 5th parameter is the mask:
If you want to set the symtrait and add it to the font, iOS will probably apply sthg like newTrait = oldTrait | (value&mask), setting the bit corresponding to mask to the value of value.
If you want to unset the symtrait and remove it from the font, you use the value of 0 as the 4th parameter and iOS will probably apply sthg like newTrait = oldTrait & ~mask to unset the bit.
But if you need to, you can also set and unset multiple bits (thus multiple symbolic traits) at once, using the right value that have 1 on bits to set and 0 on bits to unset (or to ignore), and and using the right mask that have 1 on bits that needs to be modified and 0 on bits that don't need to be changed.
[EDIT2]
I finally managed to find the solution for your specific case: you need to get the symtraits mask of your font as you already do… and bitwise-or it with the symtraits of your newFontWithoutTraits font.
This is because newFontWithoutTraits actually do have default symtraits (contrary to what I thought, it has a non-zero CTFontSymbolicTraits value) as the symtraits value also contains info for the font class and such things (so even a non-bold, non-italic font can have a non-zero symtraits value, log the hex value of the symtraits of your font to understand better).
So this is the code you need:
CTFontRef font = CTFontCreateWithName((CFStringRef)#"Courier Bold", 12, NULL);
CGFloat fontSize = CTFontGetSize(font);
CTFontSymbolicTraits fontTraits = CTFontGetSymbolicTraits(font);
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFStringRef)#"Arial", fontSize, NULL);
fontTraits |= CTFontGetSymbolicTraits(newFontWithoutTraits);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, fontSize, NULL, fontTraits, fontTraits);
// Check the results (yes, this NSLog create leaks as I don't release the CFStrings, but this is just for debugging)
NSLog(#"font:%#, newFontWithoutTraits:%#, newFont:%#", CTFontCopyFullName(font), CTFontCopyFullName(newFontWithoutTraits), CTFontCopyFullName(newFont));
// Clear memory (CoreFoundation "Create Rule", objects needs to be CFRelease'd)
CFRelease(newFont);
CFRelease(newFontWithoutTraits);
CFRelease(font);

Non-deprecated replacement for NSCalibratedBlackColorSpace?

I'm implementing my own NSBitmapImageRep (to draw PBM image files). To draw them, I'm using NSDrawBitmap() and passing it the NSCalibratedBlackColorSpace (as the bits are 1 for black, 0 for white).
Trouble is, I get the following warning:
warning: 'NSCalibratedBlackColorSpace' is deprecated
However, I couldn't find a good replacement for it. NSCalibratedWhiteColorSpace gives me an inverted image, and there seems to be no way to get NSDrawBitmap() to use a CGColorSpaceRef or NSColorSpace that I could create as a custom equivalent to NSCalibratedBlackColorSpace.
I've found a (hacky) way to shut up the warning (so I can still build warning-free until a replacement becomes available) by just passing #"NSCalibratedBlackColorSpace" instead of the symbolic constant, but I'd rather apply a correct fix.
Anybody have an idea?
OK, so I tried
NSData* pixelData = [[NSData alloc] initWithBytes: bytes +imgOffset length: [theData length] -imgOffset];
CGFloat black[3] = { 0, 0, 0 };
CGFloat white[3] = { 100, 100, 100 };
CGColorSpaceRef calibratedBlackCS = CGColorSpaceCreateCalibratedGray( white, black, 1.8 );
CGDataProviderRef provider = CGDataProviderCreateWithCFData( UKNSToCFData(pixelData) );
mImage = CGImageCreate( size.width, size.height, 1, 1, rowBytes, calibratedBlackCS, kCGImageAlphaNone,
provider, NULL, false, kCGRenderingIntentDefault );
CGColorSpaceRelease( calibratedBlackCS );
but it's still inverted. I also tried swapping black/white above, didn't change a thing. Am I misinterpreting the CIE tristimulus color value thing? Most docs seem to assume you know what it is or are willing to transform a piece of matrix maths to figure out what color is what. Or something.
I'd kinda like to avoid having to touch all the data once and invert it, but right now it seems like the best choice (/me unpacks his loops and xor operators).
It seems to have just been removed with no replacement. The fix is probably to just invert all the bits and use NSCalibratedWhiteColorSpace.
I'd create a calibrated gray CGColorSpace with the white and black points exchanged, then create a CGImage with the raster data and that color space. If you want a bitmap image rep, it's easy to create one of those around the CGImage.
Another way (since you need to support planar data and you can require 10.6) would be to create an NSBitmapImageRep in the usual way in the NSCalibaratedWhiteColorSpace, and then replace its color space with an NSColorSpace created with the CGColorSpace whose creation I suggested in my other answer.

Color.ToArgb relation between 5046311 and 14221235?

the form backcolor is 14221235 , but when i set the customcolor in colordialog to equal the form backcolor, it sets it to 5046311 !!! what is the problem?
this is how i am getting the background color:
get_background = Str(Abs(Form1.BackColor.ToArgb))
the reason i am turning it into a string is because i will feed it into a string which has "32498239, 234234234, 23423234, 32234432432, 423324234"
then i take this string and put it in customcolors like this. btw this piece of code works fine:
Dim numberStrings = My.Settings.mytext1.Split(","c).Select(Function(x) x.Trim())
ColorDialog1.CustomColors = numberStrings.Select(Function(x) CInt(x)).ToArray()
a user below mentioned that toargb takes into account the opacity. this is an excellent point indeed, and i want to clarify that i DO NOT need the opacity. how would i do toargb without taking into opacity?
this is what you want
Microsoft.VisualBasic.RGB(Me.BackColor.R, Me.BackColor.G, Me.BackColor.B).ToString
The 32-bit result from .ToArgb() contains not just the three visible color components (red, green and blue) but also the alpha component, which is essentially opacity. This is a pure guess on my part, but I think the ColorDialog is just used for picking RGB values, so when you set the color to the form's BackColor, the dialog just ignores the alpha component (or sets it to zero), which is why you end up getting a different number from the .ToArgb() method.
Note: this is just speculation on my part. It would help if you posted a code sample that demonstrates the specific problem.
I don't really understand the question. You want to set the custom color dialog CustomColor property to (the form's backcolor) r + g + b components? Not sure why you would do that, you can always just get the form's backcolor, set the Alpha value to 255 and then set the result to the CustomColor property:
Color c = Color.FromArgb( 255, form1.BackColor );
myColorDlg.CustomColor = c;
Or just use form1.BackColor.ToArgb() & 0xFFFFFF (if you want the integer value).
If you are asking for ARGB (A = Alpha) then you are asking for the opacity information. Instead you could use the R, G, B Properties of Color Independently.
You could use Color.FromArgb(255, me.BackColor).ToArgb() in order to get the ARGB value of the same color with 100% opacity.
To highlight how to pass the same colour (Using Fredou's answer) from a colorDialog to set a pie chart segment colour, one which is a .Net embedded chart and the other an Excel chart:
embchartPie.Series(0).Points(Index).Color = ColorDialog1.Color
With ColorDialog1.Color
xl_Pie_Chart.SeriesCollection(1).points(Index + 1).format.fill.forecolor.rgb = RGB(.R, .G, .B).ToString
End With