<commit_before>---
layout: post
title:  "A white (and red and green) Christmas"
date:   2016-10-24 09:55:05 PM
categories: rust,electronics
image: rgw-header.jpg
---

Here's a quick one; I'm making a Christmas display out of a bunch of serially controllable APA106 RGB LEDs, but how do I turn a value of `0 – 255` into a glorious RGW (Red Green White) struct with the correct colour, and the correct wrapping rules?

## First, some theory

The code in the next section is based on the RGB linear colour wheel approximation from the [Adafruit NeoPixel library](https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino#L123). So that I can make modifications to the generated colours, it helps to first visualise the RGB brightnesses against time. For a rainbow RGB pattern, that looks a bit like this:

![Linear RGB graph](/content/images/rgb-linear.png)

We'll get onto the RGW stuff in a sec.

## Code

I'm writing this project in Rust, however my code is modified from [the original RGB wheel C code from Adafruit](https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino#L123).

### RGB

Here's what the reference RGB function looks like in C:

```rust
uint32_t Wheel(byte WheelPos) {
	WheelPos = 255 - WheelPos;<commit_msg>Oh it _is_ C<commit_after>---
layout: post
title:  "A white (and red and green) Christmas"
date:   2016-10-24 09:55:05 PM
categories: rust,electronics
image: rgw-header.jpg
---

Here's a quick one; I'm making a Christmas display out of a bunch of serially controllable APA106 RGB LEDs, but how do I turn a value of `0 – 255` into a glorious RGW (Red Green White) struct with the correct colour, and the correct wrapping rules?

## First, some theory

The code in the next section is based on the RGB linear colour wheel approximation from the [Adafruit NeoPixel library](https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino#L123). So that I can make modifications to the generated colours, it helps to first visualise the RGB brightnesses against time. For a rainbow RGB pattern, that looks a bit like this:

![Linear RGB graph](/content/images/rgb-linear.png)

We'll get onto the RGW stuff in a sec.

## Code

I'm writing this project in Rust, however my code is modified from [the original RGB wheel C code from Adafruit](https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino#L123).

### RGB

Here's what the reference RGB function looks like in C:

```c
uint32_t Wheel(byte WheelPos) {
	WheelPos = 255 - WheelPos;
{'fraction_non_alphanumeric': 0.08404009252120277, 'fraction_numerical': 0.02467232074016962, 'mean_word_length': 4.5212765957446805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433673', 'n_tokens_mistral': 878, 'n_tokens_neox': 744, 'n_words': 344}