Hex color function for IOS in Kotlin-Multiplatform - kotlin-multiplatform

This is for whoever needs it, it is a function that converts a hex string and returns a UIColor.
This is for whoever needs it, it is a function that converts a hex string and returns a UIColor.
import kotlinx.cinterop.UIntVar
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import platform.CoreGraphics.CGFloat
import platform.Foundation.NSScanner
import platform.Foundation.scanHexInt
import platform.UIKit.UIColor
import kotlinx.cinterop.alloc
import kotlinx.cinterop.value
fun hexToUIColor(hexStr: String): UIColor {
var cString: String = hexStr.toUpperCase()
if (cString.startsWith("#")) {
cString = cString.removePrefix("#")
}
if (cString.length != 8) {
return UIColor.grayColor
}
var a: UInt
var r: UInt
var g: UInt
var b: UInt
memScoped {
val scanner = NSScanner(cString)
var pointed : UIntVar = alloc<UIntVar>()
scanner.scanHexInt(pointed.ptr)
val alpha: CGFloat = 1.0
val pointedValue: UInt = pointed.value
a = ((pointedValue and 4278190080u) shl 24) / 255u
r = ((pointedValue and 16711680u) shl 16) / 255u
g = ((pointedValue and 65280u) shl 8) / 255u
b = ((pointedValue and 255u) shl 0) / 255u
return UIColor(red = r.toDouble(), green = g.toDouble(), blue = b.toDouble(), alpha = a.toDouble())
}
}

Related

Must a zig function called at compile-time be pure?

https://ziglang.org/documentation/master/#struct
// Functions called at compile-time are memoized. This means you can
// do this:
try expect(LinkedList(i32) == LinkedList(i32));
IIUC, if functions called at compile-time are memoized, they should be pure function.
But it seems this is promised by another mechanism: type valued expression must be comptime expression(raise "unable to evaluate constant expression" if any non-comptime expression is used) + there is no way to make a global comptime var(mutable).
But there is a "counterexample":
const std = #import("std");
const expect = std.testing.expect;
test {
comptime var x: comptime_int = undefined;
const ns = struct {
fn foo() type {
return struct {
const v: i32 = x;
};
}
};
x = 10;
const a = ns.foo();
x = 20;
const b = ns.foo();
try expect(a == b);
try expect(a.v == 20);
try expect(b.v == 20);
}
But if the first access of v is before x = 20, the a.v and b.v will be 10.
const std = #import("std");
const expect = std.testing.expect;
test {
comptime var x: comptime_int = undefined;
const ns = struct {
fn foo() type {
return struct {
const v: i32 = x;
};
}
};
x = 10;
const a = ns.foo();
try expect(a.v == 10);
x = 20;
const b = ns.foo();
try expect(a == b);
try expect(a.v == 10);
}

How to generate very sharp color scale for below zero and above zero?

I'm encountering a big problem when using the number 0 (zero) as a factor for the colors to generate scales, the numbers close to 0 (zero) end up becoming almost white, impossible to see a difference.
The idea is that above 0 (zero) it starts green and gets even stronger and below 0 (zero) starting with a red one and getting stronger.
I really need any number, even if it's 0.000001 already has a visible green and the -0.000001 has a visible red.
Link to SpreadSheet:
https://docs.google.com/spreadsheets/d/1uN5rDEeR10m3EFw29vM_nVXGMqhLcNilYrFOQfcC97s/edit?usp=sharing
Note to help with image translation and visualization:
Número = Number
Nenhum = None
Valor Máx. = Max Value
Valor Min. = Min Value
Current Result / Expected Result
After reading your new comments I understand that these are the requisites:
The values above zero should be green (with increased intensity the further beyond zero).
The values below zero should be red (with increased intensity the further beyond zero).
Values near zero should be coloured (not almost white).
Given those requisites, I developed an Apps Script project that would be useful in your scenario. This is the full project:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Extra").addItem("Generate gradient", "parseData").addToUi();
}
function parseData() {
var darkestGreen = "#009000";
var lighestGreen = "#B8F4B8";
var darkestRed = "#893F45";
var lighestRed = "#FEBFC4";
var range = SpreadsheetApp.getActiveRange();
var data = range.getValues();
var biggestPositive = Math.max.apply(null, data);
var biggestNegative = Math.min.apply(null, data);
var greenPalette = colourPalette(darkestGreen, lighestGreen, biggestPositive);
var redPalette = colourPalette(darkestRed, lighestRed, Math.abs(
biggestNegative) + 1);
var fullPalette = [];
for (var i = 0; i < data.length; i++) {
if (data[i] > 0) {
var cellColour = [];
cellColour[0] = greenPalette[data[i] - 1];
fullPalette.push(cellColour);
} else if (data[i] < 0) {
var cellColour = [];
cellColour[0] = redPalette[Math.abs(data[i]) - 1];
fullPalette.push(cellColour);
} else if (data[i] == 0) {
var cellColour = [];
cellColour[0] = null;
fullPalette.push(cellColour);
}
}
range.setBackgrounds(fullPalette);
}
function colourPalette(darkestColour, lightestColour, colourSteps) {
var firstColour = hexToRGB(darkestColour);
var lastColour = hexToRGB(lightestColour);
var blending = 0.0;
var gradientColours = [];
for (i = 0; i < colourSteps; i++) {
var colour = [];
blending += (1.0 / colourSteps);
colour[0] = firstColour[0] * blending + (1 - blending) * lastColour[0];
colour[1] = firstColour[1] * blending + (1 - blending) * lastColour[1];
colour[2] = firstColour[2] * blending + (1 - blending) * lastColour[2];
gradientColours.push(rgbToHex(colour));
}
return gradientColours;
}
function hexToRGB(hex) {
var colour = [];
colour[0] = parseInt((removeNumeralSymbol(hex)).substring(0, 2), 16);
colour[1] = parseInt((removeNumeralSymbol(hex)).substring(2, 4), 16);
colour[2] = parseInt((removeNumeralSymbol(hex)).substring(4, 6), 16);
return colour;
}
function removeNumeralSymbol(hex) {
return (hex.charAt(0) == '#') ? hex.substring(1, 7) : hex
}
function rgbToHex(rgb) {
return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
function hex(c) {
var pool = "0123456789abcdef";
var integer = parseInt(c);
if (integer == 0 || isNaN(c)) {
return "00";
}
integer = Math.round(Math.min(Math.max(0, integer), 255));
return pool.charAt((integer - integer % 16) / 16) + pool.charAt(integer % 16);
}
First of all the script will use the Ui class to show a customised menu called Extra. That menu calls the main function parseData, that reads the whole selection data with getValues. That function holds the darkest/lightest green/red colours. I used some colours for my example, but I advise you to edit them as you wish. Based on those colours, the function colourPalette will use graphical linear interpolation between the two colours (lightest and darkest). That interpolation will return an array with colours from darkest to lightest, with as many in-betweens as the maximum integer in the column. Please notice how the function uses many minimal functions to run repetitive tasks (converting from hexadecimal to RGB, formatting, etc…). When the palette is ready, the main function will create an array with all the used colours (meaning that it will skip unused colours, to give sharp contrast between big and small numbers). Finally, it will apply the palette using the setBackgrounds method. Here you can see some sample results:
In that picture you can see one set of colours per column. Varying between random small and big numbers, numerical series and mixed small/big numbers. Please feel free to ask any doubt about this approach.
A very small improvement to acques-Guzel Heron
I made it skip all non numeric values, beforehand it just errored out.
I added an option in the menu to use a custom range.
Thank you very much acques-Guzel Heron
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Extra')
.addItem('Generate gradient', 'parseData')
.addItem('Custom Range', 'customRange')
.addToUi();
}
function parseData(customRange = null) {
const darkestGreen = '#009000';
const lighestGreen = '#B8F4B8';
const darkestRed = '#893F45';
const lighestRed = '#FEBFC4';
let range = SpreadsheetApp.getActiveRange();
if (customRange) {
range = SpreadsheetApp.getActiveSpreadsheet().getRange(customRange);
}
const data = range.getValues();
const biggestPositive = Math.max.apply(null, data.filter(a => !isNaN([a])));
const biggestNegative = Math.min.apply(null, data.filter(a => !isNaN([a])));
const greenPalette = colorPalette(darkestGreen, lighestGreen, biggestPositive);
const redPalette = colorPalette(darkestRed, lighestRed, Math.abs(biggestNegative) + 1);
const fullPalette = [];
for (const datum of data) {
if (datum > 0) {
fullPalette.push([greenPalette[datum - 1]]);
} else if (datum < 0) {
fullPalette.push([redPalette[Math.abs(datum) - 1]]);
} else if (datum == 0 || isNaN(datum)) {
fullPalette.push(['#ffffff']);
}
}
range.setBackgrounds(fullPalette);
}
function customRange() {
const ui = SpreadsheetApp.getUi();
result = ui.prompt("Please enter a range");
parseData(result.getResponseText());
}
function colorPalette(darkestColor, lightestColor, colorSteps) {
const firstColor = hexToRGB(darkestColor);
const lastColor = hexToRGB(lightestColor);
let blending = 0;
const gradientColors = [];
for (i = 0; i < colorSteps; i++) {
const color = [];
blending += (1 / colorSteps);
color[0] = firstColor[0] * blending + (1 - blending) * lastColor[0];
color[1] = firstColor[1] * blending + (1 - blending) * lastColor[1];
color[2] = firstColor[2] * blending + (1 - blending) * lastColor[2];
gradientColors.push(rgbToHex(color));
}
return gradientColors;
}
function hexToRGB(hex) {
const color = [];
color[0] = Number.parseInt((removeNumeralSymbol(hex)).slice(0, 2), 16);
color[1] = Number.parseInt((removeNumeralSymbol(hex)).slice(2, 4), 16);
color[2] = Number.parseInt((removeNumeralSymbol(hex)).slice(4, 6), 16);
return color;
}
function removeNumeralSymbol(hex) {
return (hex.charAt(0) == '#') ? hex.slice(1, 7) : hex;
}
function rgbToHex(rgb) {
return '#' + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
function hex(c) {
const pool = '0123456789abcdef';
let integer = Number.parseInt(c, 10);
if (integer === 0 || isNaN(c)) {
return '00';
}
integer = Math.round(Math.min(Math.max(0, integer), 255));
return pool.charAt((integer - integer % 16) / 16) + pool.charAt(integer % 16);
}

Function to find the duplicate characters in a string with their number of occurrences in the Dart language

How can we find duplicate characters in a string with their number of occurrences? The function should be generic in the Dart language.
Sample:
Input = “abcBCAacd”
Output = “a”: 3
“B” : 2 , “c” :3
void main(List<String> args) {
var input = 'abcBCAacd'.toLowerCase().split('');
var list1 = input.toSet();
var myMap = Map<String, int>.fromIterables(list1, List.generate(list1.length, (i) => 0));
input.forEach((e) => myMap[e] = myMap[e]! + 1);
print(myMap);
}
Result:
{a: 3, b: 2, c: 3, d: 1}
I had to presume you wanted the d counted, and that everything should be folded to lowercase. If B and b are in different buckets, just remove the toLowerCase() from below:
void main() {
var input = 'abcBCAacd';
var chars = input.toLowerCase().split('');
var counts = <String, int>{};
for (var char in chars) {
counts[char] = (counts[char] ?? 0) + 1;
}
print(counts);
}
void main() {
int nbOccurence = 0;
String input ="abcdeffff";
for( var i = 0 ; i < input.length; i++ ) {
nbOccurence = 0;
for( var j = 0 ; j < input.length; j++ ) {
if (input[i] == input[j]){
nbOccurence++;
}
}
print(input[i] + ":" + nbOccurence.toString());
}
}

How to measure percentages of colored areas over background? ImageJ?

I would like to measure the percentage of purple area of this gray map.
I can say roughly speaking that represents the 10% of the area, but I need an exact value.
I know there is a free tool called ImageJ but I don't know how to use it.
Maybe you can suggest me another tool or how to perform such task.
Thanks in advance.
You could write a small ImageJ plugin for that task:
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.plugin.filter.ExtendedPlugInFilter;
import ij.plugin.filter.PlugInFilterRunner;
import ij.process.ImageProcessor;
import ij.IJ;
import ij.ImagePlus;
import java.awt.AWTEvent;
import java.awt.Label;
import java.lang.Math;
public class Purple_Counter implements ExtendedPlugInFilter, DialogListener{
private final int FLAGS = DOES_RGB | PARALLELIZE_STACKS;
private boolean preview = true;
private ImagePlus im;
private int count;
private double total;
private int color = 0xff64318a;
private int background = 0xffd7d7d7;
private double maxDistanceColor;
private double maxDistanceBckg ;
public int setup(String args, ImagePlus im){
this.im = im;
maxDistanceColor = 60;
maxDistanceBckg = 60;
return FLAGS;
}
public void run(ImageProcessor ip){
int height = ip.getHeight();
int width = ip.getWidth();
count = 0;
total = 0;
for(int a = 0; a < height; a++){
for(int b = 0; b < height; b++){
int p = ip.getPixel(a,b);
if ( calculateDistance(p,color) < maxDistanceColor){
count++;
ip.putPixel(a,b,0xffff0000);
}
if ( calculateDistance(p,background) < maxDistanceBckg){
total++;
ip.putPixel(a,b,0xff000000);
}
}
}
IJ.log("Counted: " + count + "| Total: " + total);
IJ.log("Percentage: " + (count/total) * 100f);
IJ.log("");
}
private double calculateDistance(int color1, int color2){
int red1 = (color1 >> 16)&0xff;
int red2 = (color2 >> 16)&0xff;
int green1 = (color1 >> 8)&0xff;
int green2 = (color2 >> 8)&0xff;
int blue1 = color1 & 0xff;
int blue2 = color2 & 0xff;
double distance = Math.sqrt((red1 * red1 - 2*red1*red2 + red2*red2) + (green1 * green1 - 2*green1*green2 + green2*green2) + (blue1 * blue1 - 2*blue1*blue2 + blue2*blue2));
return distance;
}
public int showDialog(ImagePlus imp, String Command, PlugInFilterRunner pfr){
//Dialog to control the used color distances
GenericDialog gd = new GenericDialog("Distance Settings");
gd.addMessage("Please chose maximal distances to identify POI and background pixels: ");
gd.addSlider("POI Red: ", 0,255,0x64);
gd.addSlider("POI Green: ", 0,255,0x31);
gd.addSlider("POI Blue: ", 0,255,0x8a);
gd.addSlider("BCKG Red: ", 0,255,0xd7);
gd.addSlider("BCKG Green: ", 0,255,0xd7);
gd.addSlider("BCKG Blue: ", 0,255,0xd7);
gd.addSlider("POI max. Distance: ", 0.001, 1000, maxDistanceColor);
gd.addSlider("BCKG max. Distance: ", 0.001, 1000, maxDistanceBckg);
gd.addPreviewCheckbox(pfr, "Preview");
gd.addDialogListener(this);
gd.showDialog();
if(gd.wasCanceled() ||!dialogItemChanged(gd, null)){
return DONE;
}
return IJ.setupDialog(imp, FLAGS);
}
public boolean dialogItemChanged(GenericDialog gd, AWTEvent e){
int alpha = 0xff;
int colorRed = (int)gd.getNextNumber();
int colorGreen = (int)gd.getNextNumber();
int colorBlue = (int)gd.getNextNumber();
int bckgRed = (int)gd.getNextNumber();
int bckgGreen = (int)gd.getNextNumber();
int bckgBlue = (int)gd.getNextNumber();
color = (alpha << 24 | colorRed << 16 | colorGreen << 8 | colorBlue);
background = (alpha << 24 | bckgRed << 16 | bckgGreen << 8 | bckgBlue);
maxDistanceColor = gd.getNextNumber();
maxDistanceBckg = gd.getNextNumber();
return true;
}
public void setNPasses(int nPasses){
//Has to be implemented for ExtendedPlugInFilter
}
}
This plugin will open the following dialog:
This dialog allows to set both the pixel of interest (POI) color as well as the background (bckg) color.Via setting the distances manually, one can decide what the plugin determines as POI or as background.
The reference colors are as following:
POI:
Background:
To verify you are using the right distance settings, i recommend to use the preview function. While using that, ImageJ will color every pixel it considers as POI red, whilst every background pixel will be colored black. I got the following result using the settings given in the dialog:
As you can see, due to the artifacting some of the white pixels are also considered as background. Also the text is considered as background, so i recommend to remove it before using the plugin. If get rid of it, the result slightly changes to around 4.2%.

Testcase failed after converting codes from Objective-C to Swift

I am doing some bitwise operations in Swift style, which these codes are originally written in Objective-C/C. I use UnsafeMutablePointer to state the beginning index of memory address and use UnsafeMutableBufferPointer for accessing the element within the scope.
You can access the original Objective-C file Here.
public init(size: Int) {
self.size = size
self.bitsLength = (size + 31) / 32
self.startIdx = UnsafeMutablePointer<Int32>.alloc(bitsLength * sizeof(Int32))
self.bits = UnsafeMutableBufferPointer(start: startIdx, count: bitsLength)
}
/**
* #param from first bit to check
* #return index of first bit that is set, starting from the given index, or size if none are set
* at or beyond its given index
*/
public func nextSet(from: Int) -> Int {
if from >= size { return size }
var bitsOffset = from / 32
var currentBits: Int32 = bits[bitsOffset]
currentBits &= ~((1 << (from & 0x1F)) - 1).to32
while currentBits == 0 {
if ++bitsOffset == bitsLength {
return size
}
currentBits = bits[bitsOffset]
}
let result: Int = bitsOffset * 32 + numberOfTrailingZeros(currentBits).toInt
return result > size ? size : result
}
func numberOfTrailingZeros(i: Int32) -> Int {
var i = i
guard i != 0 else { return 32 }
var n = 31
var y: Int32
y = i << 16
if y != 0 { n = n - 16; i = y }
y = i << 8
if y != 0 { n = n - 8; i = y }
y = i << 4
if y != 0 { n = n - 4; i = y }
y = i << 2
if y != 0 { n = n - 2; i = y }
return n - Int((UInt((i << 1)) >> 31))
}
Testcase:
func testGetNextSet1() {
// Passed
var bits = BitArray(size: 32)
for i in 0..<bits.size {
XCTAssertEqual(32, bits.nextSet(i), "\(i)")
}
// Failed
bits = BitArray(size: 34)
for i in 0..<bits.size {
XCTAssertEqual(34, bits.nextSet(i), "\(i)")
}
}
Can someone guide me why the second testcase fail but the objective-c version pass ?
Edit: As #vacawama mentioned: If you break testGetNextSet into 2 tests, both pass.
Edit2: When I run tests with xctool, and tests which calling BitArray's nextSet() will crash while running.
Objective-C version of numberOfTrailingZeros:
// Ported from OpenJDK Integer.numberOfTrailingZeros implementation
- (int32_t)numberOfTrailingZeros:(int32_t)i {
int32_t y;
if (i == 0) return 32;
int32_t n = 31;
y = i <<16; if (y != 0) { n = n -16; i = y; }
y = i << 8; if (y != 0) { n = n - 8; i = y; }
y = i << 4; if (y != 0) { n = n - 4; i = y; }
y = i << 2; if (y != 0) { n = n - 2; i = y; }
return n - (int32_t)((uint32_t)(i << 1) >> 31);
}
When translating numberOfTrailingZeros, you changed the return value from Int32 to Int. That is fine, but the last line of the function is not operating properly as you translated it.
In numberOfTrailingZeros, replace this:
return n - Int((UInt((i << 1)) >> 31))
With this:
return n - Int(UInt32(bitPattern: i << 1) >> 31)
The cast to UInt32 removes all but the lower 32 bits. Since you were casting to UInt, you weren't removing those bits. It is necessary to use bitPattern to make this happen.
Finally I found out that startIdx just need to be initialized after allocation.
self.startIdx = UnsafeMutablePointer<Int32>.alloc(bitsLength * sizeof(Int32))
self.startIdx.initializeFrom(Array(count: bitsLength, repeatedValue: 0))
Or use calloc with just one line code:
self.startIdx = unsafeBitCast(calloc(bitsLength, sizeof(Int32)), UnsafeMutablePointer<Int32>.self)
Furthermore, I use lazy var to defer the Initialization of UnsafeMutableBufferPointer until the property is first used.
lazy var bits: UnsafeMutableBufferPointer<Int32> = {
return UnsafeMutableBufferPointer<Int32>(start: self.startIdx, count: self.bitsLength)
}()
On the other hand, don't forget to deinit:
deinit {
startIdx.destroy()
startIdx.dealloc(bitsLength * sizeof(Int32))
}

Resources