Converting from ushort to RGB values - math

I'm trying to read an image file which has its colors stored in the following fashion:
Hex RGB
00 00 -> 0/0/0
01 00 -> 0/0/8
02 00 -> 0/0/16
...
20 00 -> 0/8/0
21 00 -> 0/8/8
22 00 -> 0/8/16
As you can see, it will increase the previous channel if the current one has a value of 256 or bigger.
I for the world can't seem to figure out a simple algorithm to calculate the appropriate color. If anyone can whip that for me I would be very thankful!
Update:
0x00 -> 0x1F == 0x20 -> 0x3F == 0/0/0 -> 0/0/248
0x40 -> 0x5F == 0x60 -> 0x7F == 0/8/0 -> 0/8/248
I just noticed that 32 ranges that follow up produce same color result... this is not some efficient RGB encoding, it's more like a protection if you ask me.
Another update:
I ended up making a colormap which I can use to lookup the right color for a specific number, you can see it's a pretty weird looking map. If someone can figure out an algorithm to get to the colors specified in this map I'd be very grateful as I'm really curious to how it works.

It looks to me as if they have done a 5-5-5 or a 5-6-5 bit level encoding to maximize the color capability within 16 bits. what this means is for lets say a 5-6-5 that the bit encoding would look like the below.
---------------------------------------------------------------------------------
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---------------------------------------------------------------------------------
| red | green | blue |
---------------------------------------------------------------------------------
However in your case it also looks as if they swapped byte order meaning that yours based upon the order you gave could actually look as follows (bytes ordered as you show them):
| byte 0 | byte 1 |
---------------------------------------------------------------------------------
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---------------------------------------------------------------------------------
| low green | blue | red | hi green |
---------------------------------------------------------------------------------

Disclaimer: This is a repost of an update to my original question for the sake of completion.
I ended up making a colormap which I can use to lookup the right color for a specific number, you can see it's a pretty weird looking map. If someone can figure out an algorithm to get to the colors specified in this map I'd be very grateful as I'm really curious to how it works.

Related

Turn markdown table into html table

I´m currently using a tool, which creates a little informational report over solidity smart contracts with some usefull informations in it, that I want to partially display on the website I´m working on.
The tool is called solidity-metrics (https://github.com/ConsenSys/solidity-metrics) and can be used as CLI or as a library in your backend for example, which is what I want to use.
I´m sending files to my backend, which than scans those solidity files with the metrics tool.
It creates can create an object with a lot of meta-data using metrics.total() but it also can create a markdown string using metrics.generateReportMarkdown().
It includes taledata, which is only displayed in the markdown, not in the metrics.total() object, so I need to somehow turn it into html.
The table content from the markdown looks like this:
| Type | File | Logic Contracts | Interfaces | Lines | nLines | nSLOC | Comment Lines | Complex. Score | Capabilities |
|========|=================|============|=======|=======|===============|==============|
| 📝 | ./uploads/contracts/ProjectName.sol | 1 | **** | 564 | 535 | 308 | 152 | 221 | **<abbr title='Uses Assembly'>🖥</abbr><abbr title='Payable Functions'>💰</abbr><abbr title='Uses Hash-Functions'>🧮</abbr><abbr title='create/create2'>🌀</abbr>** |
| 📝 | ./uploads/contracts/ERC2981.sol | 1 | **** | 48 | 37 | 24 | 7 | 15 | **** |
| 🔍 | ./uploads/contracts/IERC2981.sol | **** | 1 | 18 | 14 | 3 | 10 | 3 | **** |
| 📝 | ./uploads/contracts/MockERC721.sol | 1 | **** | 27 | 27 | 19 | 1 | 18 | **** |
| 📝 | ./uploads/contracts/TokenName.sol | 1 | **** | 285 | 262 | 157 | 46 | 159 | **<abbr title='Uses Assembly'>🖥</abbr><abbr title='Uses Hash-Functions'>🧮</abbr>** |
| 📝 | ./uploads/contracts/Originals.sol | 1 | **** | 142 | 128 | 83 | 22 | 81 | **<abbr title='Uses Hash-Functions'>🧮</abbr>** |
| 📝 | ./uploads/contracts/SutterTreasury.sol | 1 | **** | 23 | 23 | 17 | 1 | 20 | **<abbr title='Payable Functions'>💰</abbr>** |
| 📝🔍 | **Totals** | **6** | **1** | **1107** | **1026** | **611** | **239** | **517** | **<abbr title='Uses Assembly'>🖥</abbr><abbr title='Payable Functions'>💰</abbr><abbr title='Uses Hash-Functions'>🧮</abbr><abbr title='create/create2'>🌀</abbr>** |
What I´ve tried and found out so far:
So the table actually kinda looks like normal markdown to me, but somehow, when trying to throw it inside a .md files it still doesn´t look like it should:
I figured out, that they have linked this github.css file in their credits:
https://gist.github.com/tuzz/3331384
tryed to import it into the .md file but it also didn´t seem to work, don´t even know if that makes sense to import a css file into a .md file...
To come to an end, does anyone have an idea on how to turn this markdown into html, which looks like the image I posted above (which I created with the function, which you can build into vs code, where you can mark the files, right click and scan them using metrics)?
Edit: So I figured out, that the equal signs, splitting the headers from the table body could be replaced by hyphens, which would work out fine in the markdown. I could solve that by replacing all "=" with "-", but that would destroy the rest of the markdown file, because there are also other usecases for the equal sign.

Add category column to a data set

I've a data table like this
+------------+-------+
| Model | Price |
+------------+-------+
| Apple-1 | 10 |
+------------+-------+
| New Apple | 11 |
+------------+-------+
| Orange | 13 |
+------------+-------+
| Orange2019| 15 |
+------------+-------+
| Cat | 19 |
+------------+-------+
I'want to define a list of base model tags that I want to add to any single row that matches certain condition/value. So for example defined a data frame for tagging like this
+------------+--------+
| Model | Tag |
+------------+------ -+
| Apple-1 | A |
+------------+------ -+
| New Apple | A |
+------------+------ -+
| Orange | B |
+------------+------ -+
| Cat | B |
+------------+--------+
I would like to find some way to get this results:
+------------+-------+--------+
| Model | Price | Tag |
+------------+-------+--------+
| Apple-1 | 10 | A |
+------------+-------+--------|
| New Apple | 11 | A |
+------------+-------+--------|
| Orange | 13 | B |
+------------+-------+--------|
| Orange2019| 15 | B |
+------------+-------+--------|
| Cat | 19 | B |
+------------+-------+--------|
I'm don't mind to use a table to managed the tagging data, and I know that I could write very "ad-hoc" mutate statement to achieve the results I want, just wondering if there is more elegant way to tagging a string based on a pattern match.
One idea is to use the Levenshtein distances to cluster the words you have. You would need to provide with a number of clusters. Once you have this clusters, just add the number of each one as a category tag to your table. Check out this answer which goes into detail of Levenshtein distance clustering. Text clustering with Levenshtein distances
edit
I think I totally misunderstood your question... try this
df=data.frame("Model"=c("Apple-1","New Apple","Organe","Orange2019","Cat"),
"Price"=c(10,11,13,15,19),stringsAsFactors = FALSE)
tags=data.frame("Model"=c("Apple-1","New Apple","Orange","Cat"),
"Tag"=c("A","A","B","B"),stringsAsFactors = FALSE)
df%>%rowwise()%>%mutate(Tag=if_else(!is.na(tags$Tag[which(!is.na(str_extract(Model,tags$Model)))[1]]),
tags$Tag[which(!is.na(str_extract(Model,tags$Model)))[1]],false="None"))
Model Price Tag
<chr> <dbl> <chr>
1 Apple-1 10 A
2 New Apple 11 A
3 Organe 13 None
4 Orange2019 15 B
5 Cat 19 B
I actually changed Orange for Organe so that you see what happens if there is not match ( none is returned)

Shift planning with Linear Programming

The Modeling and Solving Linear Programming with R book has a nice example on planning shifts in Sec 3.7. I am unable to solve it with R. Also, I am not clear with the solution provided in the book.
Problem
A company has a emergency center which is working 24 hours a day. In
the table below, is detailed the minimal needs of employees for each of the
six shifts of four hours in which the day is divided.
Shift Employees
00:00 - 04:00 5
04:00 - 08:00 7
08:00 - 12:00 18
12:00 - 16:00 12
16:00 - 20:00 15
20:00 - 00:00 10
R solution
I used the following to solve the above.
library(lpSolve)
obj.fun <- c(1,1,1,1,1,1)
constr <- c(1,1,0,0,0,0,
0,1,1,0,0,0,
0,0,1,1,0,0,
0,0,0,1,1,0,
0,0,0,0,1,1,
1,0,0,0,0,1)
constr.dir <- rep(">=",6)
constr.val <-c (12,25,30,27,25,15)
day.shift <- lp("min",obj.fun,constr,constr.dir,constr.val,compute.sens = TRUE)
And, I get the following result.
> day.shift$objval
[1] 1.666667
> day.shift$solution
[1] 0.000000 1.666667 0.000000 0.000000 0.000000 0.000000
This is nowhere close to the numerical solution mentioned in the book.
Numerical solution
The total number of solutions required as per the numerical solution is 38. However, since the problem stated that, there is a defined minimum number of employees in every period, how can this solution be valid?
s1 5
s2 6
s3 12
s4 0
s5 15
s6 0
Your mistake is at the point where you initialize the variable constr, because you don't define it as a matrix. Second fault is your matrix itself. Just look at my example.
I was wondering why you didn't stick to the example in the book because I wanted to check my solution. Mine is based on that.
library(lpSolve)
obj.fun <- c(1,1,1,1,1,1)
constr <- matrix(c(1,0,0,0,0,1,
1,1,0,0,0,0,
0,1,1,0,0,0,
0,0,1,1,0,0,
0,0,0,1,1,0,
0,0,0,0,1,1), ncol = 6, byrow = TRUE)
constr.dir <- rep(">=",6)
constr.val <-c (5,7,18,12,15,10)
day.shift <- lp("min",obj.fun,constr,constr.dir,constr.val,compute.sens = TRUE)
day.shift$objval
# [1] 38
day.shift$solution
# [1] 5 11 7 5 10 0
EDIT based on your question in the comments:
This is the distribution of the shifts on the periods:
shift | 0-4 | 4-8 | 8-12 | 12-16 | 16-20 | 20-24
---------------------------------------------------
20-4 | 5 | 5 | | | |
0-8 | | 11 | 11 | | |
4-12 | | | 7 | 7 | |
8-16 | | | | 5 | 5 |
12-20 | | | | | 10 | 10
18-24 | | | | | |
----------------------------------------------------
sum | 5 | 16 | 18 | 12 | 15 | 10
----------------------------------------------------
need | 5 | 7 | 18 | 12 | 15 | 10
---------------------------------------------------

How to decode the application extension block of GIF?

How to decode the application extension block of GIF?
0000300: 73e7 d639 bdad 10ad 9c08 b5a5 0021 ff0b s..9.........!..
0000310: 4e45 5453 4341 5045 322e 3003 0100 0000 NETSCAPE2.0.....
0000320: 21f9 0409 1900 f600 2c00 0000 0016 01b7 !.......,.......
this "
21 ff0b s..9.........!..
0000310: 4e45 5453 4341 5045 322e 30
" is known, but what is "03 0100 0000"?
The following describes GIF Netscape Application extension, taken from here.
The block is 19 bytes long. First 14 bytes belongs to general
Application Extension format, syntax is described in GIF89a
Specification, section "26. Application Extension".
Syntax
0 | 0x21 | Extension Label
+---------------+
1 | 0xFF | Application Extension Label
+---------------+
2 | 0x0B | Block Size
+---------------+
3 | |
+- -+
4 | |
+- -+
5 | |
+- -+
6 | |
+- NETSCAPE -+ Application Identifier (8 bytes)
7 | |
+- -+
8 | |
+- -+
9 | |
+- -+
10 | |
+---------------+
11 | |
+- -+
12 | 2.0 | Application Authentication Code (3 bytes)
+- -+
13 | |
+===============+ --+
14 | 0x03 | Sub-block Data Size |
+---------------+ |
15 | 0x01 | Sub-block ID |
+---------------+ | Application Data Sub-block
16 | | |
+- -+ Loop Count (2 bytes) |
17 | | |
+===============+ --+
18 | 0x00 | Block Terminator
You already know the data up to NETSCAPE2.0. The next byte 0x03 tells us the next data sub-block length which is always 3 bytes. The following 0x01 is the sub-block ID. For Netscape block, there is only one data sub-block and the ID is 1.
The following 2 bytes specifies the loop count in little endian — how many times the image frames should be looped, which is 0, and 0 means loop forever.
The last byte 0x00 is used to terminate the data block. So when we meet a 0x00 where data sub-block length should be, we know there are no sub-blocks left and we need to stop reading the block.

Hexadecimal Multiplication

Is there any shortcut or a some quick way of multiplying 2 small Hexadecimal numbers apart from converting into decimal ? Like in pen and paper method
Thanks,
Kiran
When you learn to multiply in base 10, you're taught to memorize multiplication-tables. The base 10 table is as follows:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
--+---+---+---+---+---+---+---+---
2 | 4 | 6 | 8 |10 |12 |14 |16 |18
--+---+---+---+---+---+---+---+---
3 | 6 | 9 |12 |15 |18 |21 |24 |27
--+---+---+---+---+---+---+---+---
etc...
When you're multiplying in other bases, you perform the same shortcuts, using a different multiplication-table (base 16):
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F
--+---+---+---+---+---+---+---+---+---+---+---+---+---+---
2 | 4 | 6 | 8 | A | C | E |10 |12 |14 |16 |18 |1A |1C |1E
--+---+---+---+---+---+---+---+---+---+---+---+---+---+---
3 | 6 | 9 | C | F |12 |15 |18 |1B |1E |21 |24 |27 |2A |2D
etc...
Long hand binary math is done the same way as longhand decimal for adding just carry the 2.
1010110 x 101
Add these numbers
1010110 ones column
00000000 tens column (or 2s column)
101011000 100s column (or 4s column)
=========
110101110
You didn't mention a platform/language/ect.
EDIT: OP clarified "pen and paper" after I wrote this.
Windows calculator has hex, octal, and binary modes.
But ultimately, numbers in a computer are base 2. Tools/languages which support decimal, hexidecimal, etc. are doing so for the convenience of the ape sitting at the keyboard, but in the computer's memory the number ends up being base 2.
For instance, in C the following two statements are the same (after lexing):
int x = 0xf * 0xf0; // hexidecimal
int x = 017 * 0360; // octal
int x = 15 * 240; // decimal
The different notations are for the convenience of the programmer, but in the machine these numbers are all represented the same way.
Using linux? You can use dc to do hex math. Set the input and output radix to 16 and you good to go.

Resources