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 (C) 2024 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/dcmemory.h>
28
29#include <gal/color4d.h>
30
31
32using namespace KIGFX;
33
34
35void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, const COLOR4D& aBackground,
36 const COLOR4D& aColor )
37{
38 wxMemoryDC bmpDC;
39 wxBrush brush;
40
41 // Prepare Bitmap
42 bmpDC.SelectObject( aLayerbmp );
43
44 brush.SetStyle( wxBRUSHSTYLE_SOLID );
45
46 if( aBackground != COLOR4D::UNSPECIFIED )
47 {
48 brush.SetColour( aBackground.WithAlpha( 1.0 ).ToColour() );
49 bmpDC.SetBrush( brush );
50 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
51 }
52
53 brush.SetColour( aColor.ToColour() );
54 bmpDC.SetBrush( brush );
55 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
56
57 bmpDC.SetBrush( *wxTRANSPARENT_BRUSH );
58 bmpDC.SetPen( *wxBLACK_PEN );
59 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
60}
61
62
63void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, int aLayer ) const
64{
66 const COLOR4D color = getLayerColor( aLayer );
67
68 DrawColorSwatch( aLayerbmp, bgColor, color );
69}
70
71
72static constexpr unsigned BM_LAYERICON_SIZE = 24;
74 // 0 = draw pixel with white
75 // 1 = draw pixel with black
76 // 2 = draw pixel with top layer from router pair
77 // 3 = draw pixel with bottom layer from router pair
78 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3 },
79 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3 },
80 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3 },
81 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3 },
82 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3 },
83 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3 },
84 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3 },
85 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3 },
86 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
87 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 3, 3,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101 { 2, 2, 2, 2, 2, 0, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
102};
103
104static COLOR4D ICON_WHITE{ 0.86, 0.86, 0.86, 1.0 };
105static COLOR4D ICON_BLACK{ 0.28, 0.28, 0.28, 1.0 };
106
107
108std::unique_ptr<wxBitmap> LAYER_PRESENTATION::CreateLayerPairIcon( const COLOR4D& aBgColor, const COLOR4D& aTopColor,
109 const COLOR4D& aBottomColor, int aScale )
110{
111 auto layerPairBitmap = std::make_unique<wxBitmap>( BM_LAYERICON_SIZE, BM_LAYERICON_SIZE );
112
113 // Draw the icon, with colors according to the router's layer pair
114 wxMemoryDC iconDC;
115 iconDC.SelectObject( *layerPairBitmap );
116 wxBrush brush;
117 wxPen pen;
118 int buttonColor = -1;
119
120 brush.SetStyle( wxBRUSHSTYLE_SOLID );
121 brush.SetColour( aBgColor.WithAlpha( 1.0 ).ToColour() );
122 iconDC.SetBrush( brush );
123 iconDC.DrawRectangle( 0, 0, BM_LAYERICON_SIZE, BM_LAYERICON_SIZE );
124
125 for( unsigned ii = 0; ii < BM_LAYERICON_SIZE; ii++ )
126 {
127 for( unsigned jj = 0; jj < BM_LAYERICON_SIZE; jj++ )
128 {
129 if( s_BitmapLayerIcon[ii][jj] != buttonColor )
130 {
131 switch( s_BitmapLayerIcon[ii][jj] )
132 {
133 default:
134 case 0: pen.SetColour( ICON_WHITE.ToColour() ); break;
135 case 1: pen.SetColour( ICON_BLACK.ToColour() ); break;
136 case 2: pen.SetColour( aTopColor.ToColour() ); break;
137 case 3: pen.SetColour( aBottomColor.ToColour() ); break;
138 }
139
140 buttonColor = s_BitmapLayerIcon[ii][jj];
141 iconDC.SetPen( pen );
142 }
143
144 iconDC.DrawPoint( jj, ii );
145 }
146 }
147
148 // Deselect the bitmap from the DC in order to delete the MemoryDC safely without
149 // deleting the bitmap
150 iconDC.SelectObject( wxNullBitmap );
151
152 // Scale the bitmap
153 wxImage image = layerPairBitmap->ConvertToImage();
154
155 // "NEAREST" causes less mixing of colors
156 image.Rescale( aScale * image.GetWidth() / 4, aScale * image.GetHeight() / 4,
157 wxIMAGE_QUALITY_NEAREST );
158
159 return std::make_unique<wxBitmap>( image );
160}
161
162
163std::unique_ptr<wxBitmap> LAYER_PRESENTATION::CreateLayerPairIcon( int aLeftLayer, int aRightLayer,
164 int aScale ) const
165{
166 const COLOR4D bgColor = getLayerColor( LAYER_PCB_BACKGROUND );
167 const COLOR4D topColor = getLayerColor( aLeftLayer );
168 const COLOR4D bottomColor = getLayerColor( aRightLayer );
169
170 return CreateLayerPairIcon( bgColor, topColor, bottomColor, aScale );
171}
int color
Definition: DXF_plotter.cpp:58
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
wxColour ToColour() const
Definition: color4d.cpp:220
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:398
static std::unique_ptr< wxBitmap > CreateLayerPairIcon(const KIGFX::COLOR4D &aBgColor, const KIGFX::COLOR4D &aTopColor, const KIGFX::COLOR4D &aBottomColor, int aScale)
Create a layer pair "side-by-side swatch" icon.
static void DrawColorSwatch(wxBitmap &aLayerbmp, const COLOR4D &aBackground, const COLOR4D &aColor)
virtual COLOR4D getLayerColor(int aLayer) const =0
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition: layer_ids.h:221
static const char s_BitmapLayerIcon[BM_LAYERICON_SIZE][BM_LAYERICON_SIZE]
static COLOR4D ICON_WHITE
static constexpr unsigned BM_LAYERICON_SIZE
static COLOR4D ICON_BLACK
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247