packages feed

colorhash (empty) → 0.1.0.0

raw patch · 6 files changed

+135/−0 lines, 6 filesdep +basedep +colourdep +hashablesetup-changed

Dependencies added: base, colour, hashable

Files

+ CHANGELOG.md view
@@ -0,0 +1,11 @@+# Changelog for `colorhash`++All notable changes to this project will be documented in this file.++The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),+and this project adheres to the+[Haskell Package Versioning Policy](https://pvp.haskell.org/).++## Unreleased++## 0.1.0.0 - YYYY-MM-DD
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright 2025 Willem Van Onsem++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1.  Redistributions of source code must retain the above copyright notice, this+    list of conditions and the following disclaimer.++2.  Redistributions in binary form must reproduce the above copyright notice,+    this list of conditions and the following disclaimer in the documentation+    and/or other materials provided with the distribution.++3.  Neither the name of the copyright holder nor the names of its contributors+    may be used to endorse or promote products derived from this software+    without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,19 @@+# colorhash++[![Build Status of the package by GitHub actions](https://github.com/hapytex/colorhash/actions/workflows/build-ci.yml/badge.svg)](https://github.com/hapytex/colorhash/actions/workflows/build-ci.yml)+[![Hackage version badge](https://img.shields.io/hackage/v/colorhash.svg)](https://hackage.haskell.org/package/colorhash)+++Hashes are an easy way to distinguish between two objects since it is not impossible that two random objects have the same hash, but it is very unlikely. But for humans, a hash is often still not very convenient. Colors are an easier way to distinguish. For example one can make a list of items, and give each of the items a different color based on the hash.++This module provides a function `rgbHash :: (Hashable a, Floating b, Ord b) => a -> Colour b` that can convert any `Hashable` object to a `Colour`.++## `colorhash` is *safe* Haskell++The only module, `Data.Hashable.Color` is compiled with the `Safe` pragma.++## Contribute++You can contribute by making a pull request on the [*GitHub repository*](https://github.com/hapytex/colorhash).++You can contact the package maintainer by sending a mail to [`yourfriends@hapytex.eu`](mailto:yourfriends@hapytex.eu).
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ colorhash.cabal view
@@ -0,0 +1,39 @@+cabal-version:       2.2++name:                colorhash+version:             0.1.0.0+synopsis: Generate a color for a hashable object.+-- description:+homepage:            https://github.com/hapytex/colorhash#readme+license:             BSD-3-Clause+license-file:        LICENSE+author:              Willem Van Onsem+maintainer:          yourfriends@hapytex.eu+copyright:           2025+category:            Web+build-type:          Simple+extra-source-files:  README.md+                     CHANGELOG.md++library+  hs-source-dirs:      src+  exposed-modules:+      Data.Hashable.Color+  build-depends:+      base >= 4.7 && < 5+    , colour >=0.0.0+    , hashable >=1.0+  default-language:    Haskell2010+  ghc-options:         -Wall+                       -Wcompat+                       -Widentities+                       -Wincomplete-record-updates+                       -Wincomplete-uni-patterns+                       -Wmissing-export-lists+                       -Wmissing-home-modules+                       -Wpartial-fields+                       -Wredundant-constraints++source-repository head+  type:     git+  location: https://github.com/hapytex/colorhash
+ src/Data/Hashable/Color.hs view
@@ -0,0 +1,38 @@+{-# LANGUAGE Safe #-}++-- |+-- Module      : Data.Hashable.Color+-- Description : A module to work convert a 'Hashable' object to a 'Colour'+-- Maintainer  : hapytexeu+gh@gmail.com+-- Stability   : experimental+-- Portability : POSIX+--+-- Hashes are an easy way to distinguish between two objects since it is not impossible that two random objects have the same hash, but it is very unlikely.+-- But for humans, a hash is often still not very convenient. Colors are an easier way to distinguish. For example one can make a list of items, and give+-- each of the items a different color based on the hash.+--+-- This module provides a function 'rgbHash' that can convert any 'Hashable' object to a 'Colour'.+module Data.Hashable.Color (rgbHash) where++import Data.Bits (shiftR, (.&.))+import Data.Colour (Colour)+import Data.Colour.SRGB (sRGB24)+import Data.Hashable (Hashable (hash))+import Data.Word (Word8)++_word8 :: Int -> Word8+_word8 = toEnum . (255 .&.)++-- | Convert a given 'Hashable' object to a 'Colour' by determining the hash, and using the last 24 bits as source for the red, green, and blue channels of the 'Colour' to construct.+rgbHash ::+  (Hashable a, Floating b, Ord b) =>+  -- | The 'Hashable' object to convert to a color.+  a ->+  -- | The corresponding color to use.+  Colour b+rgbHash x = sRGB24 r g b+  where+    h = hash x+    b = _word8 h+    g = _word8 (shiftR h 8)+    r = _word8 (shiftR h 16)