CGContext draw with two UIColor with opacity - iphone

I am drawing a line using CGContext and color has transparency of 0.7 , Now i want to draw another line with another color with same transparency on that previous line . But i get the second line color as a solid color without any transparency in the part where this both two lines intersect. For first line i am using blend effect clear to draw a transparent line and for the second line i am using blend effect color. Please tell me how to draw these two lines separately so that the second line drawn can have its own separate color .

The default blending mode (kCGBlendModeNormal) should provide the behavior you want in both cases.

Related

Flutter : Custom Painter draw path with different fill and stroke color

I am using the CustomPainter to draw a line chart where the line(stroke) needs to be of a different color and the fill color should be a different shade of it.
I could draw the chart but both with same colors.
However, I need the colors to be different.
How can I do this with a CustomPainter?
Also, I want to know how to have a single path painted with different colors instead of a single color if possible.
Thanks for your help!
I personally draw the stroke with "drawLine" calls and the fill with "drawPath".
You can define 2 different paints and use paint1 with "drawLine" and paint2 with "drawPath".

How to create a graph-paper kind of layout?

I am making a custom View where I am plotting a curve. Now I want the background of that curve to be like a graph paper.
shall I use a vertical and horizontal lines Or draw a series of rectangles Or use background image?
currently I am using vertical and horizontal lines but the problem is even when I am setting the thickness of the line to be 1 pixel, It still seems to be thicker and If I reduce the thickness to say 0.5 then the color becomes lighter than what I have set it to.
For this kind of thing, if you don't expect to have to make many, many dynamic changes to the background image, you could just use a carefully-crafted .png. You can even make the thing a single square and then use
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"template"]];
Although, if you're doing plot work, then you may want to draw the lines manually as you are. The answer to your question then is to make the stroke width 1.0 but to draw the lines at the halves: so to draw a vertical line down the 100th x pixel column, move the cursor to (99.5, 0.0) and stroke to (99.5, 480.0). CoreGraphics drawing routines draw your stroke centered on the theoretical line you create, and will antialias to physical pixels as necessary.

Only White Fill Color is Transparent in UIView

I've got a UIView that is set to opaque = NO and it all works nicely. In the drawRect I'm doing custom drawing, and this works
CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor));
CGContextFillRect(context, labelOutside);
CGContextAddRect(context, labelOutside);
but this
CGContextSetFillColor(context, CGColorGetComponents([UIColor whiteColor].CGColor));
CGContextFillRect(context, labelOutside);
CGContextAddRect(context, labelOutside);
results in NO fill being produced (you can even see other stuff that I drew on the CGContext through it). How can I draw a white fill?
Note: If I set the control not be opaque, it still doesn't work.
Why not use CGContextSetFillColorWithColor? Then you can pass the CGColor object directly instead of extracting its components and assuming it's in the same color space as the context.
Edited to add:
this works
CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor));
but this
CGContextSetFillColor(context, CGColorGetComponents([UIColor whiteColor].CGColor));
results in NO fill being produced (you can even see other stuff that I drew on the CGContext through it).
As I mentioned, this way of setting the fill color assumes that the color is in the same color space as the context.
Normally, most CGContexts are in an RGB color space. whiteColor, on the other hand, is almost certainly in a white color space, which has only one or two (white and alpha) components.
So, since the context is in an RGB color space, CGContextSetFillColor expects to be passed three or four (red, green, blue, and alpha) components. When you get the components of whiteColor's CGColor, you get a pointer to a C array holding only two components. CGContextSetFillColor will read the three or four components it wants no matter what; if you pass fewer, it may find garbage after them, or it may find zeroes.
If it finds a zero in the alpha position, then the result is that you have set the fill color to something between yellow (red=1.0, green=1.0, blue=0.0) and white (all three=1.0) with zero alpha. That's what causes the fill to not show up: You are filling with a transparent color.
The problem is that mismatch between the color space the context is in (with three or four components) and the color space the color is in (with one or two components). The solution is to remove the mismatch, which is what CGContextSetFillColorWithColor does: Since you are passing the whole CGColor object, not just an array of numbers, Core Graphics will be able to set the context's color space to that of the color object and interpret the color object's components correctly.

iPhone OpenGL to draw a solid line?

I am trying to modify the GLPaint sample from apple to draw a solid line instead of the transparent neon like line in it. I am trying to achieve the touch paint function to look something like a spray paint effect. I tried disabled the glblend but no effect. And also I am trying to make OpenGL to draw on a background image instead of the default black color. Or is it possible to make Open GL to draw on a UIImageView with transparent background ?
in the paintingView.h you will find:
#define kBrushOpacity 1.0 //the opacity of the line
#define kBrushPixelStep 1 // the density the dots
#define kBrushScale 14 //thickens of the line (1 = thickest)
#define kLuminosity 0.5 // the Luminosity. I think that this is what you have to play with
#define kSaturation 1.0 //the Saturation of the line color
changing those parameters will allow you to achieve solid line.
the settings above suppose to give you a thin solid line.
hope it will help.
shani

How do I get a colored texture brush to show up in Open GL ES on white background?

I want to draw with a texture brush, in a color of my choice, on a white background using OpenGLES.
I have a bitmap image which I use CG to load and turn into a texture. This bitmap is mostly black, but has a white circle in the center that I want to use as the "brush". In other words, I want the black part to vanish in the final compositing, but the white part to take on the color that I set using glColor.
The best I can get is with the blend parameters (GL_SRC_ALPHA, GL_ONE) after setting some opaque bright color is a faded color line on a grey (not pure white background). But when I set the background to pure white, the line isn't visible.
At least in the current situation, the black edges of the original texture don't appear. Most other blend combinations I'm trying cause either nothing to show up even on grey, or to see the entire brush including the black edges, which is no good.
Is anyone willing to explain to me how I should set up my texture and/or GL states to make the bright color show through on pure white, without showing the black texture edges at all? This might be a newbie question, but I've tried working through the blend math, and I still just don't understand how the colors are all being factored together.
Here's the image I'm using as the brush:
alt text http://www.coldcoffeeandjuice.com/OpaqueBrush.png
Here's some resulting output, when the background is grey, and the glColor4f is set to (1, 0, 0, 1), eg pure red, and the brush is used on a bunch of consecutive GL_POINTs. Note that what's good about this is that only the white part of the brush image shows the color red-- that's right. The bad parts is that the red which I want to be pure and bright is pale due to being blended with the background (?) and/or the white of the brush (?) so that it washes out entirely if the background is pure white. This uses the blend params as given above (src_alpha, one).
(source: coldcoffeeandjuice.com)
Here's what I want to see, given the pure red color (thanks Paintbrush):
(source: coldcoffeeandjuice.com)
Can anyone help me understand what I'm doing wrong?
Thanks!
Ok, so you're trying to "paint" a given colour (in this case "red") on to a background, using a mask for the brush shape.
You need to do the following before you start rendering the "paint":
First make sure your brush has an alpha channel that corresponds with its shape - that is the alpha channel should look similar to the brush image you posed.
Render with these states set (note space to get around wiki markup):
// Make the current material colour track the current color
glEnable( GL_COLOR_MATERIAL );
// Multiply the texture colour by the material colour.
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Alpha blend each "dab" of paint onto background
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
See also:
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/colormaterial.html
http://www.khronos.org/opengles/documentation/opengles1_0/html/glTexEnv.html