-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolortable.go
More file actions
119 lines (107 loc) · 3.22 KB
/
colortable.go
File metadata and controls
119 lines (107 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2025 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi
import (
"fmt"
"strings"
)
// ColorTable defines the color mapping rules used during drawing.
//
// The first index is the draw (source) color index, and the second is the
// target (background) color index.
//
// For example:
//
// colorTable[7][0] = 6
//
// means drawing color 7 over color 0 will result in color 6.
//
// See more about color tables:
//
// https://www.lexaloffle.com/bbs/?tid=149249
type ColorTable [MaxColors][MaxColors]Color
func (p ColorTable) String() string {
var s strings.Builder
for i := 0; i < len(p); i++ {
s.WriteString(fmt.Sprintf("%d :%v\n", i, p[i]))
}
return s.String()
}
// ColorTables defines 4 different ColorTable entries,
// selected based on bits 6 and 7 in the Color value.
//
// When drawing a draw (source) color over a target (background) color,
// the following operation is performed:
//
// (source | target) >> 6
//
// The result of this operation determines the index of the ColorTable to use.
var ColorTables [4]ColorTable
func init() {
ResetColorTables()
}
func ResetColorTables() {
ColorTables[0] = opaqueColorTable
ColorTables[0][0] = transparentColor
ColorTables[1] = identityColorTable
}
// RemapColor changes how the color from is rendered by replacing it with the color to.
//
// This affects all future drawing operations (sprites, shapes, text, etc.).
// It does not modify the original image or sprite data — only how colors appear on screen.
//
// For example, calling:
//
// RemapColor(1, 15)
//
// causes all pixels with color index 1 to be drawn using color 15 instead.
//
// This function updates the color tables. To reset the changes, use ResetColorTables.
func RemapColor(from, to Color) {
ColorTables[0][from] = opaqueColorTable[to]
}
// SetTransparency sets whether the given color is treated as transparent.
//
// When transparency is enabled for a color, pixels using that color will not be drawn.
// This affects all future drawing operations (sprites, shapes, text, etc.).
// It does not modify the original image or sprite data — only how colors appear on screen.
//
// For example, calling:
//
// SetTransparency(0, true)
//
// makes color 0 transparent, meaning all pixels with color index 0 will be skipped during drawing.
//
// To disable transparency for a color, pass false as the second argument.
//
// This function updates the color tables. To reset the changes, use ResetColorTables.
func SetTransparency(color Color, t bool) {
if t {
ColorTables[0][color] = transparentColor
} else {
ColorTables[0][color] = opaqueColorTable[color]
}
}
var opaqueColorTable = func() (table [MaxColors][MaxColors]Color) {
for draw := Color(0); draw < MaxColors; draw++ {
for target := 0; target < MaxColors; target++ {
table[draw][target] = draw
}
}
return
}()
var transparentColor = func() (colors [MaxColors]Color) {
for target := Color(0); target < MaxColors; target++ {
colors[target] = target
}
return
}()
// drawing has no effect
var identityColorTable = func() (table [MaxColors][MaxColors]Color) {
for draw := Color(0); draw < MaxColors; draw++ {
for target := Color(0); target < MaxColors; target++ {
table[draw][target] = target
}
}
return table
}()