Skip to content

Latest commit

 

History

History
108 lines (84 loc) · 2.63 KB

color.org

File metadata and controls

108 lines (84 loc) · 2.63 KB

Contents

Color conversion

HSV to RGB

const vec4 HSV_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);

vec3 hsv2rgb(vec3 col) {
  vec3 p = abs(fract(col.xxx + HSV_K.xyz) * 6.0 - HSV_K.www);
  return col.z * mix(HSV_K.xxx, clamp(p - HSV_K.xxx, 0.0, 1.0), col.y);
}

RGB to HSV

const vec4 RGB_K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);

vec3 rgb2hsv(vec3 c) {
  vec4 p = mix(vec4(c.bg, RGB_K.wz), vec4(c.gb, RGB_K.xy), step(c.b, c.g));
  vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
  float d = q.x - min(q.w, q.y);
  float e = 1.0e-10;
  return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

Color manipulation

Luminance

float luminance(vec3 rgb) {
  return dot(rgb, vec3(0.299, 0.587, 0.114));
}

Thresholding

float threshold(vec3 rgb, float t1, float t2) {
  return smoothstep(t1, t2, luminance(rgb));
}

Rotate hue (HSV)

vec3 rotateHueHSV(vec3 hsv, float theta) {
  float h = mod(hsv.x + mod(theta, TWO_PI) / TWO_PI, 1.0);
  if (h < 0.0) { h += 1.0; }
  return vec3(h, hsv.yz);
}

Rotate hue (RGB)

vec3 rotateHueRGB(vec3 rgb, float theta) {
  return hsv2rgb(rotateHueHSV(rgb2hsv(rgb), theta));
}

Complete namespace definition

(ns thi.ng.glsl.color
  (:require
   [thi.ng.glsl.core :as glsl :include-macros true]))

(glsl/defglsl hsv2rgb
  nil "
<<hsv2rgb>>")

(glsl/defglsl rgb2hsv
  nil "
<<rgb2hsv>>")

(glsl/defglsl luminance
  nil "
<<luminance>>")

(glsl/defglsl threshold
  [luminance] "
<<threshold>>")

(glsl/defglsl rotate-hue-hsv
  nil "
<<rotate-hue-hsv>>")

(glsl/defglsl rotate-hue-rgb
  [rgb2hsv hsv2rgb rotate-hue-hsv]
  "
<<rotate-hue-rgb>>")