Skip to content

Commit ba39eff

Browse files
authored
Merge pull request #116 from karwa/heatmap
Add a heatmap
2 parents 5d345e7 + c5cf66d commit ba39eff

File tree

167 files changed

+8606
-4494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+8606
-4494
lines changed

.swift-format

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"lineLength": 100,
4+
"indentation": {
5+
"spaces": 4
6+
},
7+
"maximumBlankLines": 1,
8+
"respectsExistingLineBreaks": true,
9+
"blankLineBetweenMembers": {
10+
"ignoreSingleLineProperties": true
11+
},
12+
"lineBreakBeforeControlFlowKeywords": false,
13+
"lineBreakBeforeEachArgument": false
14+
}

Sources/AGGRenderer/CPPAGGRenderer/CPPAGGRenderer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,9 @@ namespace CPPAGGRenderer{
481481
void get_text_size(const char *s, float size, float* outW, float* outH){
482482
font_width = font_height = size;
483483
m_contour.width(-font_weight*font_height*0.05);
484-
float x = 0;
485-
float y = 0;
484+
float x = 0;
485+
float maxY = 0;
486+
float minY = 0;
486487
// set rotation of font engine to zero before calculating text width
487488
agg::trans_affine matrix;
488489
matrix *= agg::trans_affine_rotation(agg::deg2rad(0));
@@ -496,16 +497,16 @@ namespace CPPAGGRenderer{
496497
const agg::glyph_cache* glyph = m_fman.glyph(*s);
497498
if(glyph){
498499
x+=glyph->advance_x;
499-
float height = glyph->bounds.y2 - glyph->bounds.y1;
500-
y = max(y, height);
500+
maxY = max(maxY, (float)glyph->bounds.y2);
501+
minY = min(minY, (float)glyph->bounds.y1);
501502
}
502503
++s;
503504
}
504505
}
505506
if (outW)
506507
*outW = x;
507508
if (outH)
508-
*outH = y;
509+
*outH = abs(maxY - minY);
509510
}
510511

511512
unsigned save_image(const char *s, const char** errorDesc){

Sources/QuartzRenderer/QuartzRenderer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ public class QuartzRenderer: Renderer {
485485
#endif
486486
let string = NSAttributedString(string: "\(text)", attributes: attributes)
487487
let size = string.size()
488-
return Size(width: Float(size.width), height: Float(size.height))
488+
// FIXME: 'size.height' is always too big and misaligns text.
489+
return Size(width: Float(size.width), height: s)
489490
}
490491

491492
enum WritePNGError: Error {

Sources/SVGRenderer/SVGRenderer.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,11 @@ public class SVGRenderer: Renderer{
220220
}
221221

222222
public func getTextLayoutSize(text: String, textSize size: Float) -> Size {
223-
var width: Float = 0
224-
let scaleFactor = size/100.0
225-
226-
for i in 0..<text.count {
227-
let index = text.index(text.startIndex, offsetBy: i)
228-
width = width + Float(Self.LCARS_CHAR_SIZE_ARRAY[Int(text[index].ascii!)])
223+
let width = text.reduce(into: Float(0)) { width, character in
224+
guard let asciiVal = character.ascii else { return }
225+
width += Float(Self.LCARS_CHAR_SIZE_ARRAY[Int(asciiVal)])
229226
}
230-
227+
let scaleFactor = size/100.0
231228
return Size(width: width*scaleFactor + 25, height: size)
232229
}
233230

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
public enum Mapping {}

Sources/SwiftPlot/Color.swift

Lines changed: 0 additions & 56 deletions
This file was deleted.

Sources/SwiftPlot/Color/Color.swift

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
public struct Color {
2+
public var r: Float
3+
public var g: Float
4+
public var b: Float
5+
public var a: Float
6+
public init(_ r: Float, _ g: Float, _ b: Float, _ a: Float) {
7+
self.r = r
8+
self.g = g
9+
self.b = b
10+
self.a = a
11+
}
12+
public static let transparent = Color(0, 0, 0, 0)
13+
public static let black: Color = Color(0.0, 0.0, 0.0, 1.0)
14+
public static let white: Color = Color(1.0, 1.0, 1.0, 1.0)
15+
public static let transluscentWhite: Color = Color(1.0, 1.0, 1.0, 0.7)
16+
public static let purple: Color = Color(0.5, 0.0, 0.5, 1.0)
17+
public static let lightBlue: Color = Color(0.529, 0.808, 0.922, 1.0)
18+
public static let blue: Color = Color(0.0, 0.0, 1.0, 1.0)
19+
public static let darkBlue: Color = Color(0.0, 0.0, 0.54, 1.0)
20+
public static let green: Color = Color(0.0, 0.5, 0.0, 1.0)
21+
public static let darkGreen: Color = Color(0.0, 0.39, 0.0, 1.0)
22+
public static let yellow: Color = Color(1.0, 1.0, 0.0, 1.0)
23+
public static let gold: Color = Color(1.0, 0.84, 0.0, 1.0)
24+
public static let orange: Color = Color(1.0, 0.647, 0.0, 1.0)
25+
public static let red: Color = Color(1.0, 0.0, 0.0, 1.0)
26+
public static let darkRed: Color = Color(0.54, 0.0, 0.0, 1.0)
27+
public static let brown: Color = Color(0.54, 0.27, 0.1, 1.0)
28+
public static let pink: Color = Color(1.0, 0.75, 0.79, 1.0)
29+
public static let gray: Color = Color(0.5, 0.5, 0.5, 1.0)
30+
public static let darkGray: Color = Color(0.66, 0.66, 0.66, 1.0)
31+
}
32+
33+
extension Color {
34+
35+
/// Returns a `Color` whose RBGA components are generated by the given `RandomNumberGenerator`.
36+
///
37+
public static func random<RNG: RandomNumberGenerator>(using generator: inout RNG) -> Color {
38+
return Color(
39+
.random(in: 0...1.0, using: &generator),
40+
.random(in: 0...1.0, using: &generator),
41+
.random(in: 0...1.0, using: &generator),
42+
.random(in: 0...1.0, using: &generator))
43+
}
44+
45+
/// Returns a `Color` whose RBGA components are generated by the system's default `RandomNumberGenerator`.
46+
///
47+
public static func random() -> Color {
48+
var generator = SystemRandomNumberGenerator()
49+
return Color.random(using: &generator)
50+
}
51+
52+
/// Returns a `Color` whose RGB components are given by this color, and whose alpha component is `alpha`.
53+
///
54+
public func withAlpha(_ alpha: Float) -> Color {
55+
var color = self
56+
color.a = alpha
57+
return color
58+
}
59+
60+
/// Returns a `Color` whose components are a distance `offset` between this and another color.
61+
///
62+
/// - parameters:
63+
/// - other: The color to blend with.
64+
/// - offset: The fractional distance between this color and `other`, between 0 and 1.
65+
/// A value of 0 always returns this color, and a value of 1 always returns `other`.
66+
///
67+
public func linearBlend(with other: Color, offset: Float) -> Color {
68+
return Color(
69+
(other.r - r) * offset + r,
70+
(other.g - g) * offset + g,
71+
(other.b - b) * offset + b,
72+
(other.a - a) * offset + a)
73+
}
74+
}
75+
76+
#if canImport(CoreGraphics)
77+
import CoreGraphics
78+
79+
fileprivate let RGBColorSpace = CGColorSpaceCreateDeviceRGB()
80+
81+
public extension Color {
82+
@available(tvOS 13.0, watchOS 6.0, *)
83+
var cgColor: CGColor {
84+
if #available(OSX 10.15, iOS 13.0, *) {
85+
return CGColor(
86+
srgbRed: CGFloat(r),
87+
green: CGFloat(g),
88+
blue: CGFloat(b),
89+
alpha: CGFloat(a))
90+
} else {
91+
var tuple = (CGFloat(r), CGFloat(g), CGFloat(b), CGFloat(a))
92+
return withUnsafePointer(to: &tuple) { tupPtr in
93+
return tupPtr.withMemoryRebound(to: CGFloat.self, capacity: 4) { floatPtr in
94+
return CGColor(colorSpace: RGBColorSpace, components: floatPtr)!
95+
}
96+
}
97+
}
98+
}
99+
}
100+
#endif

0 commit comments

Comments
 (0)