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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/bitmap.h>
23#include <wx/bmpbndl.h>
24#include <wx/dcmemory.h>
25#include <wx/graphics.h>
26
27#include <gal/color4d.h>
28
29using namespace KIGFX;
30
31
32void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, const COLOR4D& aBackground,
33 const COLOR4D& aColor )
34{
35 wxMemoryDC bmpDC;
36 wxBrush brush;
37
38 // Prepare Bitmap
39 bmpDC.SelectObject( aLayerbmp );
40
41 brush.SetStyle( wxBRUSHSTYLE_SOLID );
42
43 if( aBackground != COLOR4D::UNSPECIFIED )
44 {
45 brush.SetColour( aBackground.WithAlpha( 1.0 ).ToColour() );
46 bmpDC.SetBrush( brush );
47 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
48 }
49
50 brush.SetColour( aColor.ToColour() );
51 bmpDC.SetBrush( brush );
52 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
53
54 bmpDC.SetBrush( *wxTRANSPARENT_BRUSH );
55 bmpDC.SetPen( *wxBLACK_PEN );
56 bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
57 bmpDC.SelectObject( wxNullBitmap );
58}
59
60
61void LAYER_PRESENTATION::DrawColorSwatch( wxBitmap& aLayerbmp, int aLayer ) const
62{
64 const COLOR4D color = getLayerColor( aLayer );
65
66 DrawColorSwatch( aLayerbmp, bgColor, color );
67}
68
69
70// Helper function to create a single bitmap at a specific size using vector graphics
71static wxBitmap createLayerPairBitmapAtSize( const COLOR4D& aTopColor, const COLOR4D& aBottomColor, int aSize )
72{
73 wxBitmap bitmap( aSize, aSize );
74 wxMemoryDC memDC;
75 memDC.SelectObject( bitmap );
76
77 wxGraphicsContext* gc = wxGraphicsContext::Create( memDC );
78 if( !gc )
79 return bitmap;
80
81 gc->SetAntialiasMode( wxANTIALIAS_DEFAULT );
82
83 int sepTopX = aSize - aSize / 3;
84 int sepTopY = -1;
85 int sepBotX = aSize / 3 - 1;
86 int sepBotY = aSize;
87
88 // Draw left quadrilateral (top layer)
89 wxGraphicsPath topPath = gc->CreatePath();
90 topPath.MoveToPoint( 0, 0 );
91 topPath.AddLineToPoint( sepTopX, 0 );
92 topPath.AddLineToPoint( sepBotX, aSize );
93 topPath.AddLineToPoint( 0, aSize );
94 topPath.CloseSubpath();
95
96 wxBrush topBrush( aTopColor.WithAlpha( 1.0 ).ToColour() );
97 gc->SetBrush( topBrush );
98 gc->SetPen( *wxTRANSPARENT_PEN );
99 gc->DrawPath( topPath );
100
101 // Draw right quadrilateral (bottom layer)
102 wxGraphicsPath bottomPath = gc->CreatePath();
103 bottomPath.MoveToPoint( sepTopX, 0 );
104 bottomPath.AddLineToPoint( aSize, 0 );
105 bottomPath.AddLineToPoint( aSize, aSize );
106 bottomPath.AddLineToPoint( sepBotX, aSize );
107 bottomPath.CloseSubpath();
108
109 wxBrush bottomBrush( aBottomColor.WithAlpha( 1.0 ).ToColour() );
110 gc->SetBrush( bottomBrush );
111 gc->DrawPath( bottomPath );
112
113 int lineScale = std::max( 1, wxRound( aSize / 24.0 ) );
114
115 // Draw separator line with white outline and black center
116 wxPen whiteLine( *wxWHITE, 3 * lineScale );
117 gc->SetPen( whiteLine );
118 gc->StrokeLine( sepTopX, sepTopY, sepBotX, sepBotY );
119
120 wxPen blackLine( *wxBLACK, 1 * lineScale );
121 gc->SetPen( blackLine );
122 gc->StrokeLine( sepTopX, sepTopY, sepBotX, sepBotY );
123
124 delete gc;
125 memDC.SelectObject( wxNullBitmap );
126
127 return bitmap;
128}
129
130
131wxBitmapBundle LAYER_PRESENTATION::CreateLayerPairIcon( const COLOR4D& aTopColor, const COLOR4D& aBottomColor, int aDefSize )
132{
133 wxVector<wxBitmap> bitmaps;
134
135 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize ) );
136 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 1.3334 ) );
137 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 1.5 ) );
138 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 2 ) );
139 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 2.6667 ) );
140 bitmaps.push_back( createLayerPairBitmapAtSize( aTopColor, aBottomColor, aDefSize * 3 ) );
141
142 return wxBitmapBundle::FromBitmaps( bitmaps );
143}
144
145
146wxBitmapBundle LAYER_PRESENTATION::CreateLayerPairIcon( int aLeftLayer, int aRightLayer, int aDefSize ) const
147{
148 const COLOR4D topColor = getLayerColor( aLeftLayer );
149 const COLOR4D bottomColor = getLayerColor( aRightLayer );
150
151 return CreateLayerPairIcon( topColor, bottomColor, aDefSize );
152}
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
wxColour ToColour() const
Definition color4d.cpp:221
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:277
static wxBitmap createLayerPairBitmapAtSize(const COLOR4D &aTopColor, const COLOR4D &aBottomColor, int aSize)
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29