Giter VIP home page Giter VIP logo

Comments (3)

ogdenjosh avatar ogdenjosh commented on May 22, 2024 2

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.

georgalis avatar georgalis commented on May 22, 2024

from pure-bash-bible.

dylanaraps avatar dylanaraps commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.