FabGL
ESP32 VGA Controller and Graphics Library
utils.h
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 
24 #ifndef _UTILS_H_INCLUDED
25 #define _UTILS_H_INCLUDED
26 
27 
28 
29 
30 namespace fabgl {
31 
32 
33 
34 template <typename T>
35 const T & max(const T & a, const T & b)
36 {
37  return (a < b) ? b : a;
38 }
39 
40 
41 template <typename T>
42 const T & min(const T & a, const T & b)
43 {
44  return !(b < a) ? a : b;
45 }
46 
47 
48 template <typename T>
49 const T & clamp(const T & v, const T & lo, const T & hi)
50 {
51  return (v < lo ? lo : (v > hi ? hi : v));
52 }
53 
54 
55 template <typename T>
56 const T & wrap(const T & v, const T & lo, const T & hi)
57 {
58  return (v < lo ? hi : (v > hi ? lo : v));
59 }
60 
61 
62 template <typename T>
63 void swap(T & v1, T & v2)
64 {
65  T t = v1;
66  v1 = v2;
67  v2 = t;
68 }
69 
70 
71 inline bool calcParity(uint8_t v)
72 {
73  v ^= v >> 4;
74  v &= 0xf;
75  return (0x6996 >> v) & 1;
76 }
77 
78 
79 
80 
81 } // end of namespace
82 
83 
84 
85 
86 
87 
88 #endif // _UTILS_H_INCLUDED
Definition: canvas.cpp:36