quartz making opacity like in paths - objective-c

When I set opacity to 0.5,create path and draw it even on self-intersections of this path opacity is the same with other parts of this path. Is there any way to make this effect in other cases? for example when I draw 2 images of same color and opacity in their intersection there is that opacity too.

You might be able to get the effect you want by changing the blending mode before drawing the images into your context. You can use CGContextSetBlendMode to set the blending mode. You can find the list of blending modes in the CGContext Reference. I suggest trying kCGBlendModeDarken or kCGBlendModeLighten first. Try some others if neither of those does what you want.

Related

konvajs - set background color for a layer

How can I set the background color of a layer using Konva library?
I tried:
dashLayerA1.setAttr("fill", 'black');
but it does not seem to do the trick.
Only by-pass I found was to create a shape rect and assign a color to it, but not convenient for what I want to do.
Thanks.
You can't set the layer's background directly.
Creating a rectangle for the background is a valid solution, that I used by myself.
If you don't need to export that background, you can just style stage container with CSS:
stage.getContainer().style.backgroundColor = 'red';
For more info take a look here: https://konvajs.org/docs/sandbox/Canvas_Background.html

Transparent window and view using Apple's Metal

I want to create a window which is fully transparent and which can be used for drawing (On-Screen-Display). It's actually quite simple to create such a window, the one has to set the opaque property to false and fill the corresponding view with a clear color. After that the CGImage can be drawn using the graphical context to update the view and the corresponding regions with alpha channel will be transparent / semi-transparent.
I decided to make the same thing but using the Metal API, i.e. I replaced my view with a MTKView and used textures instead of CGImage to draw onto the window. I read the corresponding chapters from Apple Documentation, then I took one of the examples ("Basic Texturing") and decided to modify it in order to test if it would work:
I modifed the example by settings the opaque property of the window to NO.
In the first line of drawInMTKView function I changed the clearColor in the following way: view.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 0.0); (as shown "Devices and Commands" tutorial from docs).
Then I changed the alpha channel value of the loaded image in AAPLImage.h from dstImageData[dstPixelIndex + 3] = 255; to dstImageData[dstPixelIndex + 3] = 125;.
After all these change, I would expect that my view is fully transparent (the clear color is a fully transparent color) and that the parts of the texture with the alpha channel would have the corresponding transparency level. However the view is black by default and I cannot make parts of the view (or the whole view) transparent.
I may assume that I have to add some additional configuration to the rendering pipeline in order to make it work. However it could be that such things are not possible with Metal, but I'm not sure about that.
Also I tried to follow the suggestions from Metal MTLTexture replaces semi-transparent areas with black when alpha values that aren't 1 or 0 , but unfortunately it did not help.
There is also an isOpaque property on the view class that may need to be false. In iOS (UIView), you can simply set it, but in macOS (NSView) you need to override it to return false.

Circular images

In my application, users are allowed to choose a picture they want and this picture shows up in a picturebox.
I want to know if there was a way so that this picture that they select becomes circular instead of a square?
I want all images they select to be circle shaped.
Is there anyway to do this? Doesn't even have to be a picturebox control, anything that can accomplish this is fine
I tried this with no luck - http://www.codeproject.com/Questions/430531/circle-shaped-picture-box
Not the smartest way, but worked for me in similar task. If your picturebox is fixed size, you can create "mask" which will hide the corners.
Just draw an image in PNG with transparent circle in the middle and make the rest same color as your form. Use this image as Image propperty of each imagebox and set your image as "backgroundImage" property. This way, the image will look circle-shaped.
If you need imageboxes of different size, you can create the circle mask programmatically.
EDIT: If you will prepare a calculation of circle, you can also use it to "crop" the images to circle shape. It all depends on what you want to do with these images later.
Do you need to use them later in circle or rectangle shape?

Drawing two shadows on text (Core Graphics)

I'm tying to draw two different shadows on some text to create an embossed effect. Here's the portion of my drawInRect where I draw the text with the first shadow (all the variables used are already defined):
CGContextSetShadowWithColor(context, textInnerShadowOffset, textInnerShadowBlurRadius, textInnerShadowColor.CGColor);
[textColor setFill];
[self.text drawInRect:rect withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
But now I'm faced with the problem of drawing the second shadow. I assume I'll need to change the shadow and draw the text again, but I need to do so without adding another copy of the text.
How can I draw text without really drawing the text itself? Changing the fill color to clearColor doesn't work. I've seen people use clipping masks for this, but AFAICT that will only work for simple shapes, not text.
Alternatively, is there an easier way to draw two shadows on the same text?
Two options, depending on the exact effect you want:
If you want the first, "upper" shadow to also contribute to the second, "lower" shadow underneath it, use a transparency layer.
Set your CGContext's shadow for the "lower" shadow
Create a transparency layer using CGContextBeginTransparencyLayer
Set the context's shadow for the "upper" shadow
Draw your text
End the transparency layer using CGContextEndTransparencyLayer
(Note that transparency layers can be quite expensive. It's best to call CGContextBeginTransparencyLayerWithRect and pass in as small a rect as you can.)
If you want the shadows to be independent -- the only thing that contributes to each shadow is the text -- you'll need to use a trick.
Set up the shadow with an additional large offset, big enough so that you can draw the text outside of the bounds of your context and have the shadow land in the correct place. That way you'll see only the shadow, but not the text.
Figure out what offset is "big enough". It will probably depend on the size of the context you're drawing into (based on your view), and maybe the bounds of the text.
Or, just fudge it: pick an absurdly large value like 5000 pt.
Set up your shadow. Add the big offset to its normal y offset.
Draw the text, offset vertically by the big offset.
Repeat 1-3 for each "lower" shadow, from back to front. Afterwards, draw the text and the "uppermost" shadow last, without the offset.

NSImageView + Background Color

I have requirement like this,
Have One NSIMageView and which will be changing frequently on the timer basis, On resizing, it needs to maintain the aspect ratio, so Image may not occupy the entire frame of the Image, in such case, i need to display the background color black,
can anyone help me how can i achieve that, On googling got one approach is to have SubClass of NSImageView, but no idea, what will impact on the performance, as image is changing frequently,
Set the image view's sizing mode to Proportionate. Enclose it in an NSBox set to Custom mode with a black background. No code needed, just a case of thinking inside the box...