KiCad PCB EDA Suite
Loading...
Searching...
No Matches
layer_presentation.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <wx/bitmap.h>
27#include <wx/bmpbndl.h>
28#include <wx/dcmemory.h>
29#include <wx/graphics.h>
30
31#include <gal/color4d.h>
32
33using namespace KIGFX;
34
35
36void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, const COLOR4D& aBackground,
37 const COLOR4D& aColor )
38{
39 wxMemoryDC bmpDC;
40 wxBrush brush;
41
42 // Prepare Bitmap
43 bmpDC.SelectObject( aLayerbmp );
44
45 brush.SetStyle( wxBRUSHSTYLE_SOLID );
46
47 if( aBackground != COLOR4D::UNSPECIFIED )
48 {
49 brush.SetColour( aBackground.WithAlpha( 1.0 ).ToColour() );
50 bmpDC.SetBrush( brush );
51 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
52 }
53
54 brush.SetColour( aColor.ToColour() );
55 bmpDC.SetBrush( brush );
56 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
57
58 bmpDC.SetBrush( *wxTRANSPARENT_BRUSH );
59 bmpDC.SetPen( *wxBLACK_PEN );
60 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
61 bmpDC.SelectObject( wxNullBitmap );
62}
63
64
65void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, int aLayer ) const
66{
68 const COLOR4D color = getLayerColor( aLayer );
69
70 DrawColorSwatch( aLayerbmp, bgColor, color );
71}
72
73
74// Helper function to create a single bitmap at a specific size using vector graphics
75static wxBitmap createLayerPairBitmapAtSize( const COLOR4D& aTopColor, const COLOR4D& aBottomColor, int aSize )
76{
77 wxBitmap bitmap( aSize, aSize );
78 wxMemoryDC memDC;
79 memDC.SelectObject( bitmap );
80
81 wxGraphicsContext* gc = wxGraphicsContext::Create( memDC );
82 if( !gc )
83 return bitmap;
84
85 gc->SetAntialiasMode( wxANTIALIAS_DEFAULT );
86
87 int sepTopX = aSize - aSize / 3;
88 int sepTopY = -1;
89 int sepBotX = aSize / 3 - 1;
90 int sepBotY = aSize;
91
92 // Draw left quadrilateral (top layer)
93 wxGraphicsPath topPath = gc->CreatePath();
94 topPath.MoveToPoint( 0, 0 );
95 topPath.AddLineToPoint( sepTopX, 0 );
96 topPath.AddLineToPoint( sepBotX, aSize );
97 topPath.AddLineToPoint( 0, aSize );
98 topPath.CloseSubpath();
99
100 wxBrush topBrush( aTopColor.WithAlpha( 1.0 ).ToColour() );
101 gc->SetBrush( topBrush );
102 gc->SetPen( *wxTRANSPARENT_PEN );
103 gc->DrawPath( topPath );
104
105 // Draw right quadrilateral (bottom layer)
106 wxGraphicsPath bottomPath = gc->CreatePath();
107 bottomPath.MoveToPoint( sepTopX, 0 );
108 bottomPath.AddLineToPoint( aSize, 0 );
109 bottomPath.AddLineToPoint( aSize, aSize );
110 bottomPath.AddLineToPoint( sepBotX, aSize );
111 bottomPath.CloseSubpath();
112
113 wxBrush bottomBrush( aBottomColor.WithAlpha( 1.0 ).ToColour() );
114 gc->SetBrush( bottomBrush );
115 gc->DrawPath( bottomPath );
116
117 int lineScale = std::max( 1, wxRound( aSize / 24.0 ) );
118
119 // Draw separator line with white outline and black center
120 wxPen whiteLine( *wxWHITE, 3 * lineScale );
121 gc->SetPen( whiteLine );
122 gc->StrokeLine( sepTopX, sepTopY, sepBotX, sepBotY );
123
124 wxPen blackLine( *wxBLACK, 1 * lineScale );
125 gc->SetPen( blackLine );
126 gc->StrokeLine( sepTopX, sepTopY, sepBotX, sepBotY );
127
128 delete gc;
129 memDC.SelectObject( wxNullBitmap );
130
131 return bitmap;
132}
133
134
135wxBitmapBundle LAYER_PRESENTATION::CreateLayerPairIcon( const COLOR4D& aTopColor, const COLOR4D& aBottomColor, int aDefSize )
136{
137 wxVector<wxBitmap> bitmaps;
138
139 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize ) );
140 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 1.3334 ) );
141 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 1.5 ) );
142 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 2 ) );
143 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 2.6667 ) );
144 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 3 ) );
145
146 return wxBitmapBundle::FromBitmaps( bitmaps );
147}
148
149
150wxBitmapBundle LAYER_PRESENTATION::CreateLayerPairIcon( int aLeftLayer, int aRightLayer, int aDefSize ) const
151{
152 const COLOR4D topColor = getLayerColor( aLeftLayer );
153 const COLOR4D bottomColor = getLayerColor( aRightLayer );
154
155 return CreateLayerPairIcon( topColor, bottomColor, aDefSize );
156}
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:312
wxColour ToColour() const
Definition color4d.cpp:225
static void DrawColorSwatch(wxBitmap &aLayerbmp, const COLOR4D &aBackground, const COLOR4D &aColor)
virtual COLOR4D getLayerColor(int aLayer) const =0
static wxBitmapBundle CreateLayerPairIcon(const COLOR4D &aTopColor, const COLOR4D &aBottomColor, int aDefSize=24)
Create a layer pair "side-by-side swatch" icon.
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition layer_ids.h:281
static wxBitmap createLayerPairBitmapAtSize(const COLOR4D &aTopColor, const COLOR4D &aBottomColor, int aSize)
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33