Sunday, April 16, 2017

Cellular Automata - Rule 90 and Rule 110

A cellular automaton is a model used in computer science and mathematics. The idea is to model a dynamic system by using a number of cells. Each cell has one of several possible states. With each "turn" or iteration the state of the current cell is determined by two things: its current state, and the states of the neighbouring cells.


Source WIKIPEDIA.

Rule 90:
current pattern111110101100011010001000
new state for center cell01011010
Output generated by my freepascal program of rule 90:










Rule 110:

current pattern111110101100011010001000
new state for center cell01101110
Output generated by my freepascal program of rule 110:


Friday, April 14, 2017

RGB Convert- Bitshift - SHR


If you want to convert an hexadecimal into its RGB components using a very fast technique then you should try Logical Shift. A logical shift is a bitwise operation that shifts all the bits of its operand. The two base variants are the logical left shift (SHL) and the logical right shift (SHR). They come from the good old days of Assembler.

So, how does it work? In our example, we will obtain the components or channels of the following hexadecimal value.

#ff0cf0 base 16

We want to get from 16714992 base 10 to:
red=255 ;  green=12 ; blue=240.




Note: To simplify we will just use 16 bytes and not RGBA of 24 bytes that includes the alpha (transparency) component. 
<Sample code in Freepascal:>

Program Convert;
var
  r,g,b: longint;

procedure RGB_Convert(RGB:longint; var r,g,b:longint); 

{Byvalue reference (var r,g,b). That means that we pass variables to the functions no values.}

begin
   R := (RGB shr 16);
   G := (RGB shr 8) and 255;
   B := (RGB) and 255;
end;

Begin
  RGB_Convert($ff0cf0,r,g,b);
End.

Thursday, April 13, 2017

A Simple Scroll Function


Imagine you want to display a long list of items but you have a limited area to show them on screen. In that case, you have to resort to using scroll.  Let me explain in pseudo-code one way of doing it.

Length is a function that gives the number of items contained in an array of items.
In our example DisplayLength will tell us how many items will be shown on screen. Here, we have set its value to two.

DisplayLength = 2;

Index will be the variable that holds the pointer of the current item on the list. At the beginning, it will point to the first item on the list. Hence index=0;
(Note: Computer scientists always like to start counting at 0)

Index = 0; 

Scroll_Limit will tell us how far we can keep scrolling. When Index = Scroll-Limit that means that we have reached the end of our scroll capabilities and that all items have been displayed to the user.
This is how to calculate it.

Scroll_Limit = Length (List A) - DisplayLength

In our example. Scroll_Limit = (6 - 2) = 4.

Here is our list: 

LIST A   (6 items)                DISPLAY (2 items)

aaaaa 0  -> index points to first item;
bbbbb 1
ccccc 2    
ddddd 3  
eeeee 4    
fffff 5

Imagine that you want to go forward in your list by pressing the arrow down button  (while index <= scroll_Limit). Remember that in this example, Scroll_Limit=4. This would be the result.

Now to go back, say, with the press of the up arrow key. You just have to decrease the value of index.
index-=1; Remember to set the limit at 0. Allow the key to be processed only if index>0;

Output:




Tuesday, January 3, 2017

Learning C++ - Credit Card Verification

Saturday, October 22, 2016

Text Editor - From Scratch

Another Freepascal project of mine.  A text editor for console/terminals. The text interface was done in the fashion of the old <edit.com> for MS-DOS.




The cursor, the text and keyboard input, the buffer and the menus are all done from scratch. It is far from being complete. If only it was Summer again - sigh.



Friday, September 23, 2016

Color Picker - High Color 16bits

A screenshot of a program that I made that generates a random palette and creates shades for the designated color in High Color. You can find the program in my github repository.
From Wikipedia:

High color supports 15/16-bit for three RGB  colors. In 16-bit direct color, there can be 4 bits (16 possible levels) for each of the R, G, and B components, plus optionally 4 bits for alpha (transparency), enabling 4,096 (16 × 16 × 16) different colors with 16 levels of transparency. Or in some systems there can be 5 bits per color component and 1 bit of alpha (32768 colors, just fully transparent or not); or there can be 5 bits for red, 6 bits for green, and 5 bits for blue, for 65536 colors with no transparency.


Saturday, September 10, 2016

[B4A] Android Widget APP - To-Do List

I've been working on a widget for Android systems to be used as a To-Do list.
The widget can be resized and the activity to write the items for the list is transparent.

Basic4Android doesn't come with a feature to make widgets resizable but after some reading and patience I figured out the work-around:
You have to modify the XML files in the /Objects/res/layout so that the Width and Height properties are equal to "match_parent" to stretch the labels, panel etc and then make the files read-only so that B4A doesn't overwrite them. The files in the Objects/res/drawable should be read-only too. Finally, in the Objects/res/xml folder edit the file and add the line: 
android:resizeMode="horizontal|vertical" and also android:minResizeWidth="110dp".

Also, to make an activity transparent add the following line to the manifest editor:  
SetApplicationAttribute(android:theme, "@android:style/Theme.Translucent")

>>>>>>>>>>>> Link to the Source code and APK
Screen Caps: