From: Svjatoslav Agejenko Date: Tue, 13 Aug 2024 11:10:03 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=9fa6dcf9c25ac92c330f7e4581311b61ad1d53cf;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Miscellaneous/pal.bas b/Miscellaneous/pal.bas index f5e6faa..819d91d 100644 --- a/Miscellaneous/pal.bas +++ b/Miscellaneous/pal.bas @@ -1,116 +1,161 @@ -' An attempt to generate universally reusable color parette. +' An attempt to generate a universally reusable color palette. ' By Svjatoslav Agejenko in 2001. ' homepage: svjatoslav.eu ' email: svjatoslav@svjatoslav.eu - - -DECLARE SUB getcol (a%, b%, c%, d%) DEFINT A-Y SCREEN 13 CLS -c = 0 -FOR r = 0 TO 5 - FOR g = 0 TO 5 - FOR b = 0 TO 5 - OUT &H3C8, c - c = c + 1 - OUT &H3C9, r * 12 - OUT &H3C9, g * 12 - OUT &H3C9, b * 12 - NEXT b - NEXT g -NEXT r - -'GOTO 1 - -FOR c = 0 TO 5 - FOR b = 0 TO 5 - FOR a = 0 TO 5 - LINE (a * 5 + c * 30, b * 5)-(a * 5 + 4 + c * 30, b * 5 + 4), c * 36 + b * 6 + a, BF - NEXT a - NEXT b -NEXT c - - +' Initialize color index +colorIndex = 0 + +' Generate colors by varying red, green, and blue components from 0 to 5 +FOR redComponent = 0 TO 5 + FOR greenComponent = 0 TO 5 + FOR blueComponent = 0 TO 5 + ' Set the color for the next pixel + OUT &H3C8, colorIndex + colorIndex = colorIndex + 1 + + ' Output the RGB components to the palette registers + OUT &H3C9, redComponent * 12 + OUT &H3C9, greenComponent * 12 + OUT &H3C9, blueComponent * 12 + NEXT blueComponent + NEXT greenComponent +NEXT redComponent + +' Draw a grid of colored squares using the generated color palette +FOR colorIndex = 0 TO 5 + FOR blueComponent = 0 TO 5 + FOR redComponent = 0 TO 5 + ' Draw a square with the calculated color + LINE (redComponent * 5 + colorIndex * 30, blueComponent * 5)-_ + (redComponent * 5 + 4 + colorIndex * 30, blueComponent * 5 + 4), _ + colorIndex * 36 + blueComponent * 6 + redComponent, BF + NEXT redComponent + NEXT blueComponent +NEXT colorIndex + +' Wait for user input before proceeding a$ = INPUT$(1) -ex = -100 -ey = 0 -FOR z = 0 TO 75 STEP 15 - x1 = 50 - (z / 2) - y1 = 50 - (z * .866025) - x2 = 50 + z +' Initialize coordinates for pattern drawing +patternEx = -100 +patternEy = 0 + +' Draw a series of patterns with varying colors +FOR patternZ = 0 TO 75 STEP 15 + ' Calculate the vertices of an equilateral triangle + x1 = 50 - (patternZ / 2) + y1 = 50 - (patternZ * .866025) + x2 = 50 + patternZ y2 = 50 x3 = x1 y3 = 100 - y1 - ex = ex + 100 - IF z = 45 THEN ex = ex - 300: ey = ey + 101 + ' Move to the next starting position for the pattern + patternEx = patternEx + 100 + IF patternZ = 45 THEN + patternEx = patternEx - 300 + patternEy = patternEy + 101 + END IF + ' Draw the pattern by calculating colors based on distance from triangle vertices FOR x = 0 TO 100 FOR y = 0 TO 100 + ' Calculate color components based on distance to each vertex r = 7 - (SQR((x1 - x) ^ 2 + (y1 - y) ^ 2) / 15 + 1) g = 7 - (SQR((x2 - x) ^ 2 + (y2 - y) ^ 2) / 15 + 1) b = 7 - (SQR((x3 - x) ^ 2 + (y3 - y) ^ 2) / 15 + 1) + + ' Clamp color values within the range of 0 to 5 IF r < 0 THEN r = 0 IF g < 0 THEN g = 0 IF b < 0 THEN b = 0 IF r > 5 THEN r = 5 IF g > 5 THEN g = 5 IF b > 5 THEN b = 5 - c = r * 36 + g * 6 + b - PSET (x + ex, y + ey), c + + ' Calculate the final color index + colorIndex = r * 36 + g * 6 + b + + ' Plot the pixel with the calculated color + PSET (x + patternEx, y + patternEy), colorIndex NEXT y NEXT x -NEXT z +NEXT patternZ +' Wait for user input before proceeding a$ = INPUT$(1) -1 -ex = -100 -ey = 0 -FOR z = 0 TO 75 STEP 15 - x1 = 50 - (z / 2.5) - y1 = 50 - (z * .566025) - x2 = 50 + z / 1.5 + +' Reset starting position for the second pattern +patternEx = -100 +patternEy = 0 + +' Draw a second series of patterns using color dithering, to create +' seemingly smooth color transitions while still having only 8bit colors to work with +FOR patternZ = 0 TO 75 STEP 15 + ' Calculate the vertices of an equilateral triangle with different scaling + x1 = 50 - (patternZ / 2.5) + y1 = 50 - (patternZ * .566025) + x2 = 50 + patternZ / 1.5 y2 = 50 x3 = x1 y3 = 100 - y1 - ex = ex + 100 - IF z = 45 THEN ex = ex - 300: ey = ey + 101 + ' Move to the next starting position for the pattern + patternEx = patternEx + 100 + IF patternZ = 45 THEN + patternEx = patternEx - 300 + patternEy = patternEy + 101 + END IF + + ' Initialize accumulators for dithering color components + rSum = 0 + gSum = 0 + bSum = 0 - r1 = 0 - g1 = 0 - b1 = 0 + ' Draw the pattern by calculating average colors based on distance from triangle vertices FOR x = 0 TO 100 FOR y = 0 TO 100 + ' Calculate color components based on distance to each vertex r = 30 - (SQR((x1 - x) ^ 2 + (y1 - y) ^ 2) / 2 + 1) g = 30 - (SQR((x2 - x) ^ 2 + (y2 - y) ^ 2) / 2 + 1) b = 30 - (SQR((x3 - x) ^ 2 + (y3 - y) ^ 2) / 2 + 1) - r1 = r1 + r - g1 = g1 + g - b1 = b1 + b - r = r1 / 5 - g = g1 / 5 - b = b1 / 5 - r1 = r1 - (r * 5) - g1 = g1 - (g * 5) - b1 = b1 - (b * 5) - IF r < 0 THEN r = 0 - IF g < 0 THEN g = 0 - IF b < 0 THEN b = 0 - IF r > 5 THEN r = 5 - IF g > 5 THEN g = 5 - IF b > 5 THEN b = 5 - c = r * 36 + g * 6 + b - PSET (x + ex, y + ey), c + ' Accumulate the color components + rSum = rSum + r + gSum = gSum + g + bSum = bSum + b + + ' Calculate the average color components over a span of 5 pixels + rAvg = rSum / 5 + gAvg = gSum / 5 + bAvg = bSum / 5 + + ' Reset the sums after calculating the averages + rSum = rSum - (rAvg * 5) + gSum = gSum - (gAvg * 5) + bSum = bSum - (bAvg * 5) + + ' Clamp average color values within the range of 0 to 5 + IF rAvg < 0 THEN rAvg = 0 + IF gAvg < 0 THEN gAvg = 0 + IF bAvg < 0 THEN bAvg = 0 + IF rAvg > 5 THEN rAvg = 5 + IF gAvg > 5 THEN gAvg = 5 + IF bAvg > 5 THEN bAvg = 5 + + ' Calculate the final color index + colorIndex = rAvg * 36 + gAvg * 6 + bAvg + + ' Plot the pixel with the calculated color + PSET (x + patternEx, y + patternEy), colorIndex NEXT y NEXT x -NEXT z - -a$ = INPUT$(1) -SYSTEM +NEXT patternZ +' Wait for user input before exiting +a$ = INPUT$(1) \ No newline at end of file