0
$\begingroup$

What I'm trying to reproduce is a set of functions that convert a vector to RGB values so that the hue goes full circle around the origin. After seeing a 3Blue1Brown video about graphically solving 2D-equations which have 2D outputs, like complex functions, I wanted to replicate the algorithm in Matlab.

Video screencapture

The brightening of the colors radiating from the origin is somewhat trivial, but it's the colors I'm interested in.

I decided to fiddle around in MS Paint to see what kinds of values fully saturated RGB colors have. For a red color (brightness half the way) the values are obviously $[255,0,0]$. Similarly for green and blue. Yellow, greenblue (sorry not English :D) and pink have values like $[255, 255, 0]$ and so on. The colors in between are faded appropriately. So for example red is only present in colors ranging from blue to green via red.

What function could be used to convert, say an angle with respect to the x-axis to the amount of red in a direction? I don't care so much about the fade curve or the rotation of the plane above. Only that the colors form a full hue.

Here's a crude visualisation of each color channel:

enter image description here

Any pointers would be greatly appreciated, be them purely mathematical or actual Matlab.

$\endgroup$
  • $\begingroup$ A quick search brought up this which seems to be the exact tool you need. Note that “hsv” stands for “hue, saturation, value”. $\endgroup$ – celtschk Apr 3 '18 at 20:32
  • $\begingroup$ @celtschk Ah, of course it's done with color space transforms.. And it's easy to convert radians to go around 0..1 instead of 0..2pi. Thanks! $\endgroup$ – Felix Apr 3 '18 at 20:38
  • $\begingroup$ You're welcome. $\endgroup$ – celtschk Apr 3 '18 at 20:39
0
$\begingroup$

One can use color space transforms as mentioned in the comments to convert hue to rgb. Hue can be easily extracted from a 2D-plane as follows (in Matlab):

Let us first construct a function called hue, which will convert a 2D-vector or a 2xN array of points and give back their magnitudes and hue values from 0 to 1. We'll input a matrix of the desired points around the origin, construct an HSV image and convert it to RGB using Matlab's builtin hsv2rgb function. Now we have the final image. For an image of size 21 (range 10) the result is very neat:

Rainbow

% Size of one image quadrant
range = 10;

% Scale the brightness to fade from black around origin
brightness = @(v) 1 - 1./(50/range*v+1);

[X, Y] = meshgrid(-range:range);
len = 2*range + 1;

P = [X(:)'; Y(:)'];
[r, t] = hue(P);

% HSV space image
clear hsv
hsv(:,:,1) = vec2mat(t, len)';
hsv(:,:,2) = ones(len);
hsv(:,:,3) = vec2mat(brightness(r), len)';

rgb = hsv2rgb(hsv);
image(-range, -range, rgb)

function [r, t] = hue(v)
    [~, n] = size(v);
    r = zeros(1, n);
    t = zeros(1, n);
    for i = 1:n
        v_ = v(:,i);
        r(i) = norm(v_);
        angle = atan2(v_(2), v_(1));
        t(i) = (angle + pi) / (2*pi);
    end
end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.