Working plasma pattern
Signed-off-by: Martyn Ranyard <m@rtyn.berlin>
This commit is contained in:
		
							parent
							
								
									34bb137e7b
								
							
						
					
					
						commit
						5f8e68662f
					
				
					 1 changed files with 57 additions and 12 deletions
				
			
		|  | @ -1,9 +1,15 @@ | ||||||
| package patterns | package patterns | ||||||
| 
 | 
 | ||||||
| import "math" | import ( | ||||||
|  | 	"math" | ||||||
|  | ) | ||||||
| 
 | 
 | ||||||
| type RGBcolor = [3]byte | type RGBcolor = [3]byte | ||||||
| 
 | 
 | ||||||
|  | func floatColorToIntColor(val float64) byte { | ||||||
|  | 	return byte(val*256 - 0.5) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func plasmaRGBFromVal(val byte) (byte, byte, byte) { | func plasmaRGBFromVal(val byte) (byte, byte, byte) { | ||||||
| 	var r byte | 	var r byte | ||||||
| 	var g byte | 	var g byte | ||||||
|  | @ -24,7 +30,6 @@ func plasmaRGBFromVal(val byte) (byte, byte, byte) { | ||||||
| 	return r, g, b | 	return r, g, b | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| func LinearPlasma(l int) []byte { | func LinearPlasma(l int) []byte { | ||||||
| 	line := make([]byte, l*3) | 	line := make([]byte, l*3) | ||||||
| 	for i := 0; i < l*3; i += 3 { | 	for i := 0; i < l*3; i += 3 { | ||||||
|  | @ -37,26 +42,66 @@ func LinearPlasma(l int) []byte { | ||||||
| 	return line | 	return line | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var offset float64 = 0.5 | func linearFromVal(val byte) (byte, byte, byte) { | ||||||
|  | 	var r byte = val | ||||||
|  | 	var g byte = 0 | ||||||
|  | 	var b byte = 0 | ||||||
|  | 	return r, g, b | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func positiveModF(val float64) float64 { | ||||||
|  | 	_, val = math.Modf(val) | ||||||
|  | 	if val < 0 { | ||||||
|  | 		return val + 1.0 | ||||||
|  | 	} | ||||||
|  | 	return val | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func colorPlasmaFromFloatVal(val float64) (byte, byte, byte) { | ||||||
|  | 	var r byte | ||||||
|  | 	var g byte | ||||||
|  | 	var b byte | ||||||
|  | 	val = positiveModF(val) * 3.0 | ||||||
|  | 	if val < 1.0 { | ||||||
|  | 		r = floatColorToIntColor(val) | ||||||
|  | 		g = floatColorToIntColor(1.0 - val) | ||||||
|  | 		b = 0 | ||||||
|  | 	} else if val < 2.0 { | ||||||
|  | 		b = floatColorToIntColor(val - 1.0) | ||||||
|  | 		r = floatColorToIntColor(1.0 - (val - 1.0)) | ||||||
|  | 		g = 0.0 | ||||||
|  | 	} else { | ||||||
|  | 		g = floatColorToIntColor(val - 2.0) | ||||||
|  | 		b = floatColorToIntColor(1.0 - (val - 2.0)) | ||||||
|  | 		r = 0.0 | ||||||
|  | 	} | ||||||
|  | 	return r, g, b | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var palletteOffset float64 = 0.0 | ||||||
| 
 | 
 | ||||||
| func PlasmaPanel(w int, h int, speed uint16) [][]RGBcolor { | func PlasmaPanel(w int, h int, speed uint16) [][]RGBcolor { | ||||||
| 	grid := make([][]RGBcolor, w) | 	grid := make([][]RGBcolor, w) | ||||||
| 	for i := 0; i < w; i++ { | 	for i := 0; i < w; i++ { | ||||||
| 		grid[i] = make([]RGBcolor, h) | 		grid[i] = make([]RGBcolor, h) | ||||||
| 	} | 	} | ||||||
| 	scale := math.Pi * 2.0 / float64(w) | 
 | ||||||
| 	step := float64(5 * speed / 40) | 	// pbrook didn't say what DT is for... so it resolves to 0.05 | ||||||
| 	offset -= step | 	palletteOffset -= 0.05 / 1.0 | ||||||
| 	if offset < 0 { | 	if palletteOffset < 0 { | ||||||
| 		offset += 1.0 | 		palletteOffset += 1.0 | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	offset := 0.5 // center offset | ||||||
|  | 	scale := math.Pi * 2.0 / float64(w) | ||||||
|  | 
 | ||||||
| 	for y := 0; y < h; y++ { | 	for y := 0; y < h; y++ { | ||||||
| 		for x := 0; x < w; x++ { | 		for x := 0; x < w; x++ { | ||||||
| 			u := math.Cos((float64(x) + offset) * scale) | 			u := math.Cos((float64(x) + offset) * scale) | ||||||
| 			v := math.Cos((float64(y) + offset) * scale) | 			v := math.Cos((float64(y) + offset) * scale) | ||||||
| 			w := math.Cos((float64(w) + offset) * scale) | 			j := math.Cos(offset * scale) // 2D - No Z | ||||||
| 			e := (u + v + w + 3.0) / 6.0 | 			e := (u + v + j + 3.0) / 6.0 | ||||||
| 			r, g, b := plasmaRGBFromVal(byte((offset + e) * 255)) | 			r, g, b := colorPlasmaFromFloatVal(palletteOffset + e) | ||||||
| 			var rgb [3]byte | 			var rgb [3]byte | ||||||
| 			rgb[0] = r | 			rgb[0] = r | ||||||
| 			rgb[1] = g | 			rgb[1] = g | ||||||
|  | @ -65,4 +110,4 @@ func PlasmaPanel(w int, h int, speed uint16) [][]RGBcolor { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return grid | 	return grid | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue