Agg color line segment Generator

Source: Internet
Author: User
Document directory
  • Gradient color
  • Gradient mode
  • At the end of this section, we will introduce several other color class line segment generators.
Color line segment generator header file
  1. # Include <agg_span_solid.h>
  2. # Include <agg_span_gradient.h>
  3. # Include <agg_span_gradient_alpha.h>
  4. # Include <agg_span_gouraud_gray.h>
  5. # Include <agg_span_gouraud_rgba.h>
Type
  1. Template <class colort>
  2. Class Identifier: span_solid;
  3. Template <class colort, class interpolator, class gradientf, class colgrading>
  4. Class Identifier: span_gradient;
  5. Template <class colort, class interpolator, class gradientf, class alphaf>
  6. Class Identifier: span_gradient_alpha;
  7. Template <class colort>
  8. Class Identifier: span_gouraud _ [gray | rgba];

If you see this from the above pattern line segment generator, the color class is much simpler. Similarly, we will first write a sample code to facilitate subsequent experiments.

Sample Code

Also based on this code (http://www.cppprog.com/2009/0816/146.html ),

// Rendering Buffer
Expiration: rendering_buffer & rbuf = rbuf_window ();
Authorization: pixfmt_bgr24 pixf (rbuf );

// Renderers
Typedef identifier: renderer_base <identifier: pixfmt_bgr24> renderer_base_type;
Renderer_base_type renb (pixf );

Typedef Plugin: renderer_scanline_aa_solid <renderer_base_type> renderer_scanline_type;
Renderer_scanline_type rensl (renb );

// Vertex Source
Vertex: ellipse ell (100,100, 50, 50 );

// Scanline Rasterizer
Authorization: rasterizer_scanline_aa <> RAS;
Authorization: scanline_u8 SL;

 

 

Add the following header file

# Include "agg_span_allocator.h"

# Include "agg_span_gradient.h"

 

Add the following code at the end of the on_draw () method:

// Color line segment generator demo
// Line Divider
Typedef partition: span_allocator <partition: rgba8> span_allocator_type; // Distributor Type
Span_allocator_type span_alloc; // span_allocator
// Interpolation tool
Typedef connector: span_interpolator_linear <> interpolator_type; // interpolation type
Transform: trans_affine img_ctx; // Transformation Matrix
Interpolator_type IP (img_ctx); // interpolation tool
// Gradient mode
Typedef labels: gradient_radial_focus gradientf_type;
Gradientf_type GRF (1, 0.1, 0.5 );
// Gradient color
Typedef identifier: gradient_linear_color <identifier: rgba8> colorf_type;
Colorf_type colgrading (gradient: rgba (, 1), gradient: rgba (, 1); // white to blue
// Line segment Generator
Typedef gradient: span_gradient <gradient: rgba8,
Interpolator_type,
Gradientf_type,
Colorf_type> span_gen_type;
Span_gen_type span_gen (IP, GRF, colgrading, 0, 50 );
// Combine them into a Renderer
Expiration: renderer_scanline_aa <
Renderer_base_type,
Span_allocator_type,
Span_gen_type
> My_renderer (renb, span_alloc, span_gen );
// Matrix transformation
Img_mtx.translate (100,100 );
Img_mtx.invert (); // note the following:
// Use our Renderer to draw circles
RAS. add_path (ELL );
Expiration: render_scanlines (Ras, SL, my_renderer );

 

Display Effect

  1. Span_gradient is a template class (in most cases, callback classes). The first two template parameters, colort and interpolator, are color and interpolation. The key is the next two: gradientf is used to specify the gradient method, such as horizontal gradient, vertical gradient, circular gradient, etc.; colgrading specifies the gradient color.
  2. The gradient method selects gradient: gradient_radial_focus, which is a circular gradient method that can specify the focus.
  3. Use begin: gradient_linear_color to set the start color and end color for gradient.
  4. The first three constructors of span_gradient are interpolation, gradient mode, and gradient color. The last two digits indicate the start and end positions of the gradient. The starting and ending positions of different gradient methods have different meanings. For example, starting and ending in a Circular fill indicates the center and edge. horizontal gradient indicates left to right.
  5. The matrix transformation of the interpolation tool moves the gradient center to the (100,100) point. Remember to call the invert () method to reverse.
Gradient color

As mentioned above, the template parameter coldorf of span_gradient specifies the gradient color. gradient_linear_color is used. Which classes can be used as coldorf?

As long as the "operator [] ()" and "size ()" classes are implemented, STD: vector <rgba8> also works.

Experiment code, using STD: vector <rgba8> to achieve multi-color gradient

Change the gradient color of the sample code to the following:

...
// Gradient color
// Typedef gradient: gradient_linear_color <gradient: rgba8> colorf_type;
// Colorf_type colgrading (gradient: rgba (, 1), gradient: rgba (, 1); // white to blue
Typedef STD: vector <partition: rgba8> colorf_type;
Colorf_type colgrading (256 );
Expiration: rgba begin_color (, 1), mid_color (, 0), end_color (, 1 );
For (INT I = 0; I <128; I ++) // The first 128 is from white to red.
Colgrading [I] = begin_color.gradient (mid_color, I/128.0 );
For (INT I = 0; I <128; I ++) // After 128 from red to blue
Colgrading [I + 128] = mid_color.gradient (end_color, I/128.0 );

Display Effect

The specified vector capacity of 256 here refers to the color used. You can use more colors for smoother transitions.

In addition to using vector to implement gradient of multiple colors, we can also use the gradient_lut class provided by attention, which is much easier to use.

The header file of gradient_lut is # include <agg_gradient_lut.h>

Class declaration is

Template <class colorinterpolator, unsigned colorlutsize = 256>

Class Identifier: gradient_lut

 

Colorinterpolator is responsible for generating the middle colors of the two colors. You can directly use the transform: color_interpolator that comes with the transform.

Add multiple colors using the add_color (double offset, color_type color) method of gradient_lut. The offset indicates the offset position of the added color. The value ranges from 0 ~ Between 1.

After adding all colors, call the build_lut () method to generate a color array for gradient_lut.

Experiment code, using gradient_lut to achieve multi-color gradient

Change the gradient color of the sample code to the following:

...
// Gradient color
// Typedef gradient: gradient_linear_color <gradient: rgba8> colorf_type;
// Colorf_type colgrading (gradient: rgba (, 1), gradient: rgba (, 1); // white to blue
Typedef Syntax: gradient_lut <
Parser: color_interpolator <Parser: rgba8>
> Colorf_type;
Colorf_type colgrading;
Colopen-end. add_color (0, gradient: rgba (, 1 ));
Coldorf. add_color (0.2, rows: rgba (, 0 ));
Coldorf. add_color (0.4, encoding: rgba (, 0 ));
Coldorf. add_color (0.8, encoding: rgba (, 1 ));
Coldorf. build_lut ();
...

Display Effect


Gradient mode

In addition to gradient_radial_focus in this example, labels also provide many gradient methods, which are defined in the header file # include <agg_span_gradient.h>.

It is very easy to modify the gradient method of the Demo code, such:

...
// Gradient mode
// Typedef restart: gradient_radial_focus gradientf_type;
// Gradientf_type GRF (1, 0.1, 0.5 );
Typedef Syntax: gradient_x gradientf_type;
Gradientf_type GRF;
...

Here is a part of the pipeline that comes with the gradient mode and the Display Effect
Gradient_x Gradient_y Gradient_diamond
Gradient_xy Gradient_conic Gradient_radial
At the end of this section, we will introduce several other color class line segment generators.
  • Span_solid has nothing to say. It's just solid color filling.
  • Span_gradient_alpha is a gradient of transparency. The parameter is similar to span_gradient. The difference is that coldorf is changed to alphaf, And the return value of "operator [] ()" also changes from the color structure to the transparency value.
  • Span_gouraud_rgba Gao's triangle coloring. You must specify the three vertices and three colors of the triangle. The usage is shown in the following example.

// Color line segment generator demo
// Line Divider
Typedef partition: span_allocator <partition: rgba8> span_allocator_type; // Distributor Type
Span_allocator_type span_alloc; // span_allocator

Typedef restart: span_gouraud_rgba <restart: rgba8> span_gen_type;
Span_gen_type span_gen;
// Three colors
Span_gen.colors (
Failed: rgba (, 0 ),
Paths: rgba (0, 0 ),
Usage: rgba (, 1)
);
// Three triangle vertices
Span_gen.triangle (
100,50,
130,125,
70,125, 0
);
Expiration: renderer_scanline_aa <
Renderer_base_type,
Span_allocator_type,
Span_gen_type
> My_renderer (renb, span_alloc, span_gen );

RAS. add_path (ELL );
Expiration: render_scanlines (Ras, SL, my_renderer );

Display Effect

Www.cppprog.com

Http://hi.baidu.com/%E5%D4%E5%C6%B6%C0%D0%D0/blog/item/8b82c4f867a070d3b58f31f1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.