gretmn102 / gretmn102/FsharpMyExtension

feat(Graphics.Color): add `decToRgb` and `rgbToHex`

Open
#5 0 comments 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
F#
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

```fsharp
/// Цвет в десятичном представлении преобразовывает в RGB представление.
let decToRgb n =
let rec f (acc:int list) n i =
if i > 0 then
let m = n % 256
f (m::acc) (n / 256) (i - 1)
else
List.rev acc
f [] n 3
decToRgb System.Int32.MaxValue = [255; 255; 255]

// 15.08.2020 16:23:49
let rgbToHex (r:byte, g:byte, b:byte, a:byte) =
// 256 = 0xff
// ['0'..'9'] @ ['a'..'f']
let f x =
if x < 10 then
char (x + 48) // char (x + int '0')
else
char (x + 87) // char (10 + (int 'a' - 10))
let f = function
| [x; y] -> [f x; f y]
| [x] -> ['0'; f x]
| xs -> failwith "Something went wrong"
// f (ofDec' 16 255)
[
ofDec' 16 (int r) |> f
ofDec' 16 (int g) |> f
ofDec' 16 (int b) |> f
ofDec' 16 (int a) |> f
]
|> List.map System.String.Concat
// |> List.concat
|> System.String.Concat

open Fuchu
[]
let rgbToHexTest =
testList "rgbToHexTest" [
testCase "" <| fun _ ->
let exp = rgbToHex (10uy, 34uy, 54uy, 255uy)
Assert.Equal("", "0a2236ff", exp)
]
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.