Comments (3)
It seems to be failing with "#944B53" (148, 75, 83).
Looks like this is due to g < b when r == max, since the calculation for h will give us a negative value in this scenario.
EDIT: This seems to work as far as I've tested
# Calculate the hue based on which
# RGB value is the maximum.
((h=s == 0 ? 0
: r == max
? (g - b) * 100 / (max - min)
: g == max
? 200 + (b - r) * 100 / (max - min)
: b == max
? 400 + (r - g) * 100 / (max - min)
: 0
))
# If hue is negative, add 600.
((h=h < 0 ? 600 + h : h))
# Convert the calculation result into
# degrees. Divide by 100 to reverse the
# floating point hacks.
((h=h * 60 / 100))
from pure-bash-bible.
from pure-bash-bible.
Bonus!
The entire function inside an arithmetic block (( ))
. This really starts to look like another language entirely!
The coolest part:
-> time rgb2hsl
186 100 53
real 0m 0.00s
user 0m 0.00s
sys 0m 0.00s
rgb_to_hsl() ((
r = r < 0 ? 0 : ${1:-0},
g = g < 0 ? 0 : ${2:-0},
b = b < 0 ? 0 : ${3:-0},
r = r > 255 ? 255 : r,
g = g > 255 ? 255 : g,
b = b > 255 ? 255 : b,
r = r * 100 / 255,
g = g * 100 / 255,
b = b * 100 / 255,
min=255,
min = r < min ? r : min,
max = r > max ? r : max,
min = g < min ? g : min,
max = g > max ? g : max,
min = b < min ? b : min,
max = b > max ? b : max,
mid = max - min,
tot = max + min,
l = tot / 2,
s = min == max ? 0
: l < 50
? mid * 100 / tot
: mid * 100 / (200 - max - min),
h = s == 0 ? 0
: r == max
? (g - b) * 100 / mid
: g == max
? 200 + (b - r) * 100 / mid
: b == max
? 400 + (r - g) * 100 / mid
: 0,
h = h * 60 / 100
))
rgb_to_hsl 18 233 255
printf '%s\n' "$h $s $l"
from pure-bash-bible.
Related Issues (20)
- Alternative to dos2unix
- Inaccurate Caveat for `split()`
- Much More Compatible `reverse_array()`
- Splitting a string on a delimiter HOT 1
- Bash HOT 1
- Pedantic use of ':' "in place of a temporary variable." HOT 1
- shift at least... HOT 2
- basename with one argument fails with set -u HOT 1
- read, store, write a file including nulls
- Is this project active? HOT 4
- Typo in function date HOT 1
- Using coproc with read in non-interactive shells for sleep alternative
- Bash
- [[ ' aaaa' =~ a* ]] && echo ${BASH_REMATCH[0]} || echo no HOT 8
- for loop HOT 3
- wierd if statement HOT 1
- Is the Repository Dead? HOT 1
- Bash HOT 1
- bash read multi-line string with Process Substitution HOT 1
- UPPER CASE %02X instead of x
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pure-bash-bible.