Coder Social home page Coder Social logo

Color type about line-charts HOT 5 CLOSED

terezka avatar terezka commented on June 2, 2024 1
Color type

from line-charts.

Comments (5)

terezka avatar terezka commented on June 2, 2024 1

Already implemented now!

from line-charts.

anagrius avatar anagrius commented on June 2, 2024

@2mol Seems like a natural choice.

from line-charts.

2mol avatar 2mol commented on June 2, 2024

Turns out that the Svg library expects String for Attributes.stroke and the like. Replacing the type itself really doesn't need all that many changes. The choice is between

  1. pulling eskimoblood/elm-color-extra as an additional dependency (just for colorToHex)
  2. waiting for elm-svg to maybe use the core Color type
  3. waiting for core to provide an equivalent to colorToHex

Did I miss an option? I think neither options are great, but maybe leaving it as-is and waiting for Color to be used in svg makes the most sense?

from line-charts.

terezka avatar terezka commented on June 2, 2024

Hi! Thanks for your initiative! I already made a note about this change, but it will probably be the done in a later release, as it is not blocking. However, if you want to come up with some suggestions for this, you're more than welcome :)

from line-charts.

2mol avatar 2mol commented on June 2, 2024

Cool. Since I think that it's better to sometimes copy a little bit of code, my suggestion is a colorToHex helper function for somewhere like Internals/Util.elm. I went ahead and wrote a simple implementation.

I am aware that in addition to elm-color-extra, there is also rtfeldman/hex, BUT with the assumption that color channels are always in 0-255, the helper functions are fairly simple.

So I would like to make a case to use the code below, maybe with the goal of getting a cleaner version of it into core Color.

If people are ok with this, then I can weave it in and send a PR.

(sorry if it's bad form to paste a big chunk of code, but at least it's REPL-testable)

module ColorUtil exposing (colorToHex)

import Color exposing (Color)
import Dict exposing (Dict)


colorToHex : Color -> String
colorToHex c =
    let
        { red, green, blue } =
            Color.toRgb c
    in
        [ red, green, blue ]
            |> List.map toHex
            |> String.concat


hexDict : Dict Int String
hexDict =
    [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ]
        |> List.indexedMap (,)
        |> Dict.fromList


toHex : Int -> String
toHex n =
    let
        digit1 =
            n // 16

        digit0 =
            if digit1 /= 0 then
                n % (digit1 * 16)
            else
                n

        ( hex1, hex0 ) =
            ( Dict.get digit1 hexDict
            , Dict.get digit0 hexDict
            )
    in
        case ( hex1, hex0 ) of
            ( Just a, Just b ) ->
                a ++ b

            _ ->
                "00"

from line-charts.

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.