permanently modifying RGB gamma table - macos

The following script sets a custom gamma table for the current display. It works for about a second before the screen resets to its default profile.
#!/usr/bin/swift
import CoreGraphics
var redTable: [CGGammaValue] = [0, 0]
var greenTable: [CGGammaValue] = [1, 0]
var blueTable: [CGGammaValue] = [0, 1]
CGSetDisplayTransferByTable(CGMainDisplayID(), 2, &redTable, &greenTable, &blueTable)
sleep(5)
What causes this reset without me calling CGDisplayRestoreColorSyncSettings() directly?
How would I go about permanently adjusting the gamma table?

OK, found it. I was running flux which reconfigured the display profile every second or so. Quitting the app fixed it.

Related

How to only animate the even frames in Abaqus?

I'm working on thermal simulations in Abaqus. I only need to animate the even frame numbers, so instead of the animation going 1,2,3,4,5 I need it to go 2,4,6,8,10.
Is there a way to only show the even frames? If so, how?
Go to Result --> Active Steps/Frames and deselect the frames that you don't want to be displayed and animated.
You can easily do this using Abaqus Python script.
Following is the overview of steps:
# getting current viewport object
vpName = session.currentViewportName
viewport = session.viewports[vpName]
# get odb object from viewport
odb= viewport.displayedObject
# Get all the steps available in the odb
stepNames = odb.steps.keys()
# Create animation object
ani = session.ImageAnimation(fileName='animation' ,format=AVI)
# add required frame to the animation object
for stepName in stepNames:
stpID = odb.steps[stepName].number - 1
nfrm = len(odb.steps[stp].frames)
for frmID in range(0, nfrm, 2): # 2 --> even frames will be added
viewport.odbDisplay.setFrame(step=stpID,frame=frmID)
ani.writeFrame(canvasObjects=(viewport, ))
ani.close()

How can I make udate_idletasks work on Mac

I've written a tkinter script with animation, that works fine on Xubuntu, but when I run it on Mac, the animation doesn't work. Here's a little script that demonstrates the problem:
import tkinter as tk
from time import sleep
root = tk.Tk()
canvas = tk.Canvas(root, height=200, width = 200)
canvas.pack()
this = canvas.create_rectangle(25,25, 75, 75, fill='blue')
that = canvas.create_rectangle(125, 125, 175, 175, fill = 'red')
def blink(event):
global this, that
for _ in range(9):
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill = 'blue')
canvas.update_idletasks()
sleep(.4)
this, that = that, this
canvas.bind('<ButtonRelease-1>', blink)
root.mainloop()
This draws a red square and a blue square on a canvas. When the user clicks the canvas, the squares repeatedly switch colors. On Xubuntu, it works as intended.
On Mac, when I click the canvas, I get spinning beach ball, and after a few seconds, we see that squares have switched colors, because they switch colors an odd number of times in the code.
It seems to me that update_idletasks isn't working. Is there some way to fix this? I am running python 3.9.5 with Tk 8.6 on Big Sur.
I think what you can do is avoid tasks that will block the mainloop, in this case time.sleep(). So your code can be remade by emulating a for loop with after, and I see nothing that stops this general code from running OS independent:
count = 0 # Think of this as the `_` in for _ in range(9)
def blink(event=None):
global this, that, count
if count < 9: # Basically repeats everytime `count` is less than 9, like a for loop
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill='blue')
this, that = that, this
count += 1 # Increase count
root.after(400,blink) # Repeat this code block every 400 ms or 0.4 seconds
else:
count = 0 # After it is 9, set it to 0 for the next click to be processed
I found that using update instead of update_idletasks works on both platforms. It's my understanding though, that the latter is much preferred. See the accepted answer to this question for example. This solves my immediate problem, but does anyone know if update_idletasks ever works on the Mac?

How to move an image in Lua?

I am new to Lua programming and I am having problems while trying to move an image from a set of coordinates to another.
What I am trying to create is to be used with the X-Plane flight simulator. There is a library called SASL (www.1-sim.com), that was created to make plugins (for X-Plane) creation easir, since the default language is C++ and many people find it difficult.
In general, SASL works as a bridge between Lua and X-Plane, in general lines, the scripts you write reads some data straight from X-Plane (DataRefs) while it is running and depending on the code you wrote its execute commands or many other things that it is possible.
So, when using SASL to create cockpit/panel gauges it uses a base file, named 'avionics.lua' that works as a class and loads all gauges you create for the specific aircraft you are working on. For example my avionics.lua file looks like this:
size = { 2048, 2048 }
components = {
flaps {};
};
where, 'size' is the size that will be used for things to be drawn and components is an array of gauges, in this case the flaps gauge.
The rest of the work is to create the gauge functionality and this is done in a separate file, in my case, called 'flaps.lua'.
Within flaps.lua, is where I need to code the flaps indicator functionality which is to load 2 images: one for the back ground and the second one for the flaps indicator.
The first image is a fixed image. The second one will move throught the 'y' axis based on the flaps indicator DataRef (flapsDegree property below).
The code below when X-Plane is running displays the background image and the flaps indicator on its first stage which is 0 as you can see on the image.
size = {78,100}
local flapsDegree = globalPropertyf("sim/cockpit2/controls/flap_ratio")
local background = loadImage("gfx/Flaps.png")
local indicator = loadImage("gfx/Flaps_Indicator.png")
local flaps = get(flapsPosition)
components = {
texture { position = {945, 1011, 60, 100}, image = background},
texture { position = {959, 1097, 30, 9}, image = indicator},
}
Image
Now, the problem comes when I need to implement the logic for moving the 'indicator' image through the 'y' axis.
I have tried this code without success:
if flaps == 0.333 then
indicator.position = {959, 1075, 30, 9}
end
So how could I accomplish that?
I am not familiar with the SALS library. I just had a quick look into it.
Everything you need to know is in the manuals on the website you linked.
In particular http://www.1-sim.com/files/SASL300.pdf
Everytime your screen is updated each components draw() function will be called.
So if you want to change something dynamically you have to put that into the component's draw function.
If you open the SALS sources you'll find basic components which show you how to use that stuff. One of them is needle.lua:
-- default angle
defineProperty("angle", 0)
-- no image
defineProperty("image")
function draw(self)
local w, h = getTextureSize(get(image))
local max = w
if h > max then
max = h
end
local rw = (w / max) * 100
local rh = (h / max) * 100
drawRotatedTexture(get(image), get(angle),
(100 - rw) / 2, (100 - rh) / 2, rw, rh)
end
If you check the manual you'll find that there is not only a drawRotatedTexture function but many other functions for drawing stuff. Just play around. Try drawTexture for your purpose.
If you don't know what you have to program, open every single lua file in the library and read it together with the Lua reference manual and the SALS documentation until you understand what is going on. Once you understand the existing code you can extend it with ease.

SWIFT XCODE error

I am a complete beginner in coding. I managed to get some working code in Playground but once I moved to building a project nothing worked.
This is for OS X.
I have used the following simple lines of code in Playground and AppDelegate contexts. It runs and outputs to the console without error in Playground but returns an error in AppDelegate.swift - Expected declaration for the " for num in availcrop" line with a red arrow under the f in for.
var availcrop = [0, 1, 2, 3, 4, 5, 6, 7]
for num in availcrop
{
var ran = 1000+randInt(2000)
availcrop[num] = ran
println(availcrop[num])
}
- the println is not planned for the project. Just in the Playground for testing
Both have imported Cocoa. One runs and the other crashes. In Playground the array name avail crop is blue implying it is recognised I suppose. In the AppDelegate.swift it remains grey.
Once you're dealing with classes and objects, code generally needs to be inside some method so that it can be invoked by name.
Try:
func printRandoms {
// Your code here
}

Pygame and Tilesets, followed instructions for use, but I get "noneType" error

My version of Python is 3.2, and Pygame is 1.9.1.
I followed the first part of the instructions found here: http://wiki.sheep.art.pl/Tiled%20Map%20in%20PyGame, with a few adjustments to accommodate for my own tileset:
def load_tileset(filename, width, height):
image = pygame.image.load(filename).convert_alpha()
imageWidth, imageHeight = image.get_size()
tileSet = []
for tileX in range(0, 3):
line = []
tileSet.append(line)
for tileY in range(0, 3):
rect = (tileX*width, tileY*height, width, height)
line.append(image.subsurface(rect))
if __name__=='__main__':
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tiled Background')
screen.fill((240, 240, 255))
table = load_tileset('tableset.gif', 16, 16)
for x, row in enumerate(table):
for y, tile in enumerate(row):
screen.blit(tile, (x*24, y*24))
pygame.display.flip()
while pygame.event.wait().type != pygame.locals.QUIT:
pass
I get the Nonetype error in for x, row in enumerate(table):*. I've tried various things, even moving the tileset itself, and changing the file type. I've also tried other codes, such as range.
I have managed to load images in the past, this is just my first time trying to use a tile set. The tile set has 9 16x16 tiles, and there is some transparency in them (hence the convert_alpha()).
So, can anyone say why I got the NoneType error here? Is it something to do with the image?
(Note, the reason there's 3's in the "For tileX in range" is that it wouldn't accept the calculation as set fourth in the tutorial, so I just did the maths myself.)
Edit: Accidently typed: for x, row in range(table): instead of for x, row in enumerate(table): in description.
You aren't posting the full code, or have made a mistake: there's no for x, row in range(table) line in the code above. With this specific code, you seem to be trying to pull two variables from the range function, but it only returns one value at a time.
You probably meant to type for x, row in enumerate(table). In that case, you need to return an object when you call table = load_tileset('tableset.gif', 16, 16) because unless any given function is explicitly told to return an object, it'll return None and that's why the line is giving you trouble, because the type of None is Nonetype
Here's an example of a function returning None and then returning a string, a returned string:
def thisFuncReturnsNone():
pass
print 'what gets returned:', thisFuncReturnsNone()
>>> None
def thisFuncReturnsAString():
return 'a returned string'
print 'what gets returned a second time:', thisFuncReturnsAString()
>>> a returned string

Resources